Proper use of JavaScript regular expressions with "G" tags _ regular expressions

Source: Internet
Author: User
Now let's talk about the use of regular expressions with the "G" tag, and first we'll look at a piece of code that's easier to understand from an example.
Copy Code code as follows:

1 function A (val)
2 {
3 var re =/^\d+$/g;
4 alert (Re.lastindex);
5 return Re.test (Val);
6}
7 Alert (A (5));
8 Alert (A (6));

Run the above code, in different browsers will get different results, ie: 0 true 0 true,ff and chrome: 0 True 1 false, see here, there must be someone confused, surprised. In view of this problem, I have found two methods for the current data.

1. Use match

Copy Code code as follows:

1 function A (val)

2
3 {
4
5 var re =/^\d+$/g;
6
7 if (("" +val). Match (RE)
8
9 return true;
10
One else
12
return false;
14
15}
16
Alert (A (5));
18
Alert (A (6));

Note: To use "" +val to convert Val to a string, the specific use of match can be referred to here

2. Use a regular expression with no "G" (Var re=/^\d+$/or var re=new RegExp ("^[0-9]+$"))


Copy Code code as follows:

1 function A (val)
2
3 {
4
5 var re = new RegExp ("^[0-9]+$");//or Var re=/^\d+$/;
6
7 return Re.test (Val);
8
9}
10
Alert (A (5));
12
Alert (A (6));
14
15


Note: var re = new RegExp ("^[0-9]+$") can only be used [0-9], can not use \d, as for why, I do not understand, I hope to know what to say.

It is not difficult to see, the above two methods are from the side to consider how to solve the problem, not from the positive solution, and then we from the issue of the nature of the analysis, and give a solution.

To consider the nature of the problem, we have to start with the "G" tag, a regular expression with a "G" tag has a property lastindex, which holds an integer that declares the position of the first character after the last match text. The result of the last match was found by Method Regexp.exec () and Regexp.test (), which are the starting point for the next retrieval, in the position indicated by the Lastindex property. This allows you to iterate through all the matching text in a string by repeatedly calling both methods. This property is readable and writable. As long as the next search for the target string starts, it can be set. When the method exec () or test () can no longer find text to match, they automatically reset the Lastindex property to 0. See here we are not difficult to see the cause of the problem and the solution, as long as we in the next search before the Lastindex property reset to 0 can be as follows:


Copy Code code as follows:

1 function A (val)
2
3 {
4
5 var re =/^\d+$/g;
6
7 re.lastindex=0;//Reset lastindex to 0
8
9 return Re.test (Val);
10
11}
12
Alert (A (5));
14
Alert (A (6));


More explanations on lastindex please see here.

So far we have understood the nature of the problem, and how to better solve the problem, I hope this article for some people to help.
Related Article

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.