想做个正则的验证,用js做,可是用firebug调试显示if(rule.exec(input))这句错了,我估计是对象类型搞错了,请高人帮忙啊!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function perg()
{
var input = document.getElementById("input").value;
var rule = document.getElementById("rule").value;
document.getElementById("result").innerHTML = matches(input,rule);
}function matches(input,rule)
{
if(rule.exec(input))
{
return "true";
}
else
{
return "false";
}
}
</script>
<style>
.aa{
width:200px;
height:60px;
}#content{
width:200px;
margin:0 auto;
}
</style>
<title>js</title>
</head><body>
<div id="content">
<div class="aa">
<label>正则表达式</label>
<input type="text" id="rule" />
</div>
<div class="aa">
<label>要验证的字符串</label>
<input type="text" id="input" />
<input type="button" onclick="perg();" value="验证" />
</div>
<div class="aa">结果</div>
<div class="aa" id="result"></div>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function perg()
{
var input = document.getElementById("input").value;
var rule = document.getElementById("rule").value;
document.getElementById("result").innerHTML = matches(input,rule);
}function matches(input,rule)
{
if(rule.exec(input))
{
return "true";
}
else
{
return "false";
}
}
</script>
<style>
.aa{
width:200px;
height:60px;
}#content{
width:200px;
margin:0 auto;
}
</style>
<title>js</title>
</head><body>
<div id="content">
<div class="aa">
<label>正则表达式</label>
<input type="text" id="rule" />
</div>
<div class="aa">
<label>要验证的字符串</label>
<input type="text" id="input" />
<input type="button" onclick="perg();" value="验证" />
</div>
<div class="aa">结果</div>
<div class="aa" id="result"></div>
</div>
</body>
</html>
加了eval
eval(rule).exec(input)
不过我js 和 正则不太好 还是请高手教下原理 是为啥啊
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<script type="text/javascript">
function perg()
{
var input = document.getElementById("input").value;
var rule = new RegExp(document.getElementById("rule").value);
document.getElementById("result").innerHTML = matches(input,rule);
}function matches(input,rule)
{
if(rule.exec(input))
{
return "true";
}
else
{
return "false";
}
}
</script>
<style>
.aa{
width:200px;
height:60px;
}#content{
width:200px;
margin:0 auto;
}
</style>
<title>js</title>
</head><body>
<div id="content">
<div class="aa">
<label>正则表达式</label>
<input type="text" id="rule" />
</div>
<div class="aa">
<label>要验证的字符串</label>
<input type="text" id="input" />
<input type="button" onclick="perg();" value="验证" />
</div>
<div class="aa">结果</div>
<div class="aa" id="result"></div>
</div>
</body>
</html>
不用/把正则括起来也能用.原因是你获取的是文本.不是JS变量.
EVAL是将文本转化为JS运行的函数.