JavaScript-RegExp objects can only be used once. Solution _ javascript skills

Source: Internet
Author: User
The RegExp object is discarded after it is executed once. Therefore, each time you use RegExp, you need to create a new one. The following is an example. If you need a RegExp object, you can refer to the following code, is to identify the year, month, and day from the string. You can see that the created rYMD RegExp object is executed once and then again.

Var DateStr = "2014-9-8"; var rYMD = new RegExp ("(\ d {4} | \ d {2 }) -(\ d {2} | \ d {1})-(\ d {2} | \ d {1}) "," g "); var aRt = rYMD.exe c (DateStr); var srt1_rymd.exe c (DateStr );

After debugging, it is found that the first execution, aRt gets the returned Array (Array), but the sRt followed by it is null.

After repeated attempts, it was found that the RegExp object was discarded after being executed once.

Therefore, you must note that each time you use RegExp, you need to create a new one.

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.