js中正则表达式作为参数怎么写的?

代码如下:
js
function QQ(id,k)
{
var i=document.getElementById(id);

if(!k.test(i))
{
if(id=="QQId")
{
alert("QQ格式错误或账号位数不对!!!");
}
if(id=="QQpass")
{
alert("密码格式错误或位数不对!!!");
}

}
}
html
<div style=" margin-top:10px">
<li style=" list-style:none; width:160px; margin:0px auto; font-size:12px; height:40px">QQ号码:<input type="text" class="input" id="QQId" onblur="QQ('QQId',/^[1-9][0-9]{3,9}$/)" /></li>
<li style=" list-style:none; width:160px; margin:0px auto; font-size:12px; height:30px">QQ密码:<input type="text" class="input" id="QQpass" onblur="QQ('QQpass',/^([a-zA-Z]{1,}\w){6,12}$/)"/> </li>
<img src="images/btn_login.gif" style=" margin-left:110px" />
<p style=" font-size:12px; margin:10px 5px; line-height:25px"> 世界的改变不是少数人做了很多,而是每个人都做了一点点。伸出手,让我们把爱传递。</p>
</div>
不知道正则表达式作为参数时该怎么用?

墨色风雨
浏览 772回答 1
1回答

温温酱

k传字符串就行了,你在test方法调用之前,先用此字符串new一个正则表达式对象出来就行了,var t = new RegExp(k, 'g');t.test(i)这样就可以了。
打开App,查看更多内容
随时随地看视频慕课网APP