Use of the global (/g) modifier of the javascript Regular Expression, and the regular expression global

Source: Internet
Author: User

Use of the global (/g) modifier of the javascript Regular Expression, and the regular expression global

The/g modifier indicates a global match, and searches for all matches instead of stopping the first match. Let's take a look at a piece of classic code:

Var str = "123 # abc"; var noglobal =/abc/I; // console in non-global match mode. log (re. test (str); // output tureconsole. log (re. test (str); // output tureconsole. log (re. test (str); // output tureconsole. log (re. test (str); // output turevar re =/abc/ig; // global match console. log (re. test (str); // output tureconsole. log (re. test (str); // outputs falseconsole. log (re. test (str); // output tureconsole. log (re. test (str); // output false
We can see that when the/g mode is used, the output results of multiple RegExp. test () executions are different. The following explanation is excerpted from this blog:

 When creating a regular expression object, if I use a "g" identifier or set it? When the global attribute value is true, the newly created Regular Expression object uses the pattern to perform global matching on the string to be matched. In global match mode, you can perform multiple matches on the specified string to be searched. Each match uses the value of the lastIndex attribute of the current regular object as the starting position for starting search in the target string. The initial value of the lastIndex attribute is 0. After a matched item is found, the value of lastIndex is reset to the position index of the next character in the matching content, which is used to identify the position where the search starts when the next matching is executed. If the value of lastIndex cannot be found, it is set to 0. When the global match flag of the regular object is not set, the value of the lastIndex attribute is always 0. Only the first matching item in the string is searched for each matching operation.

The following code verifies the lastIndex performance in/g mode:

Var str = "012345678901234567890123456789"; var re =/123/g; console. log (re. lastIndex); // output 0. The regular expression is just starting to create the console. log (re. test (str); // output ture console. log (re. lastIndex); // output 4 console. log (re. test (str); // outputs true console. log (re. lastIndex); // output 14 console. log (re. test (str); // output ture console. log (re. lastIndex); // output 24 console. log (re. test (str); // outputs false console. log (re. lastIndex); // the output value is 0. No matching item is found and is reset.



We have seen the impact of the/G model on regexp.test(). Now we can see the impact on the regexp.exe c () function. RegExp.exe c () returns an array containing matching results. If no match is found, the return value is null.

Var str = "012345678901234567890123456789"; var re =/abc/; // execnot found matching lele.log(re.exe c (str); // nullconsole. log (re. lastIndex); // 0
Var str = "012345678901234567890123456789"; var re =/123/; var resultArray = re.exe c (str); console. log (resultArray [0]); // The matching result is 123console. log (resultArray. input); // target string strconsole. log (resultArray. index); // index of matching results in the original string str


For the regexp.exe c method, if g is not added, only the first match is returned, regardless of the number of executions. If g is added, the first match is returned for the first execution, then, the second match is returned, and so on.

Var str = "012345678901234567890123456789"; var re =/123/; var globalre =/123/g; // non-global match var resultArray = re.exe c (str); console. log (resultArray [0]); // output 123console. log (resultArray. index); // output 1console. log (globalre. lastIndex); // output 0var resultArray = re.exe c (str); console. log (resultArray [0]); // output 123console. log (resultArray. index); // output 1console. log (globalre. lastIndex); // output 0 // global match var resultArray = globalre.exe c (str); console. log (resultArray [0]); // output 123console. log (resultArray. index); // output 1console. log (globalre. lastIndex); // output 4var resultArray = globalre.exe c (str); console. log (resultArray [0]); // output 123console. log (resultArray. index); // output 11console. log (globalre. lastIndex); // output 14



When the/g matching mode is used, we can obtain all the matching items through loops.

Var str = "012345678901234567890123456789"; var globalre =/123/g; // loop traversal, get all matching var result = null; while (result = globalre.exe c (str ))! = Null) {console. log (result [0]); console. log (globalre. lastIndex );}


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.