正则(JS)re=new RegExp(“^\\d*$”);与re=/^\d*$/;之间区别?

以下代码结果为:FALSE,TRUE,TRUE , 结果中为什么第一个为FALSE呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<form name=form1>
字符串:<input name=”t1″ value=123456″>
模式:/<input name=”t2″ value=”^\d*$”>/
</form>
<script language=javascript>
function c1()
{
re=new RegExp(“^\d*$”);
alert(re.test(“123456″));
}
function c2(form)
{
re=new RegExp(form.t2.value);
alert(re.test(form.t1.value));
}
function c3()
{
re=/^\d*$/;
alert(re.test(“123456″));
}
c1();
c2(document.form1);
c3();
</script>

第一个应为 re=new RegExp(“^\\d*$”);
\在引号中需要转义
第一个表达式有双引号,双引号要加多一次转义的,第二个没有,这就是区别

This entry was posted in javascript and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>