Correct use of the Javascript Regular Expression with the "G" mark

Source: Internet
Author: User

Next we will talk about the use of regular expressions labeled with "g". First, let's take a look Code It is easier to understand from the examples. Copy code The Code is 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 and different results will be obtained in different browsers. in IE: 0 true 0 true, FF and chrome: 0 true 1 false. Here, someone may be confused, surprised. To solve this problem, I have found two ways to view the information.

1. Use match

Copy code The Code is 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
11 else
12
13 return false;
14
15}
16
17 alert (A (5 ));
18
19 alert (A (6 ));

Note: You need to use "" + Val to convert Val to a string. For more information about how to use match, see here.

2. Use a regular expression without "G" (VAR Re =/^ \ D + $/or var Re = new Regexp ("^ [0-9] + $ "))

Copy codeThe Code is 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
11 alert (A (5 ));
12
13 alert (A (6 ));
14
15

Note: var Re = new Regexp ("^ [0-9] + $") can only use [0-9], cannot use \ D, as to why, I don't quite understand. I hope someone can tell me what I know.

It is not hard to see that the above two methods are to consider how to solve the problem from the aspect, but not from the front. Next we will analyze the problem from the nature and give a solution.

From the perspective of the nature of the problem, we have to start with the "G" mark. The regular expression with the "G" mark has an attribute lastindex, which stores an integer, it declares the position of the first character after the last match of the text. The last matching result is found by the regexp.exe C () and Regexp. Test () methods. Both of them use the location indicated by the lastindex attribute as the start point of the next search. In this way, you can call these two methods repeatedly to traverse all matched texts in a string. This attribute is readable and writable. You can set the target string as long as the next search starts. When the exec () or test () method can no longer find the matching text, they will automatically reset the lastindex attribute to 0. Here we can easily see the cause and solution of the problem, as long as we reset the lastindex attribute to 0 before the next search, as shown below:

Copy codeThe Code is 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
13 alert (A (5 ));
14
15 alert (A (6 ));

For more information about lastindex, see here.

So far, we have understood the nature of the problem and how to better solve it. I hope this article will be helpful to some people.

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.