What is the difference between regular (JS) Re = new Regexp ("^ \ D * $") and Re =/^ \ D * $?

Source: Internet
Author: User

Below CodeResult: false, true, true
Why is the first result false?
<Form name = form1>
String: <input name = "T1" value = "123456">
Mode:/<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>
The first should beRe = new Regexp ("^ \ D * $ ");
\ Escape in quotation marks
The first pair has double quotation marks. The double quotation marks must be escaped once. The second pair does not exist. This is the difference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.