Home >Web Front-end >JS Tutorial >Detailed explanation of the use of the g modifier of the regular global matching pattern
This time I will bring you a detailed explanation of the use of the regular global matching pattern g modifier. What are the precautions when using the regular global matching pattern g modifier. The following is a practical case, let's take a look.
Regular expressiong modifier:
g modifier specifies that the regular expression performs global matching, that is, after finding the first The search will continue after matching.
Syntax structure:
ConstructorMethod:
new RegExp("regexp","g")
Object direct method:
/regexp/g
Browser support:
IE browser supports this metacharacter.
Firefox supports this metacharacter.
Google Chrome supports this metacharacter.
Example code:
Example 1:
var str="this is an antzone good"; var reg=/an/; console.log(str.match(reg));
The above code can only match the first "an" because there is no global matching. After the first match is successful, no further matches are made.
Example 2:
var str="this is an antzone good"; var reg=/an/g; console.log(str.match(reg));
The above code can match two "an".
The following is a supplement
This article will introduce in detail the usage of global matching pattern/g of regular expressions in js. The code is as follows:
var str = "123#abc"; var re = /abc/ig; console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出false console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出false
In When creating a regular expression object, if the "g" identifier is used or its global attribute value is set to true, the newly created regular expression object will use the pattern to match the characters. string for global matching. In global matching mode, multiple matches can be performed on the specified string to be found. Each match uses the value of the lastIndex attribute of the current regular object as the starting position to start searching in the target string. The initial value of the lastIndex attribute is 0. After a matching item is found, the value of lastIndex is reset to the position index of the next character of the matching content in the string. It is used to identify the position to start searching when the next matching is performed. If it cannot be found, The value of lastIndex of the matched item will be set to 0. When the global matching flag of the regular object is not set, the value of the lastIndex attribute is always 0, and each time a match is performed, only the first matching item in the string is found. You can use the following code to check the value of the corresponding lastIndex attribute during execution. The code is as follows:
var str = "123#abc"; var re = /abc/ig; console.log(re.test(str)); //输出ture console.log(re.lastIndex); //输出7 console.log(re.test(str)); //输出false console.log(re.lastIndex); //输出0 console.log(re.test(str)); //输出ture console.log(re.lastIndex); //输出7 console.log(re.test(str)); //输出false console.log(re.lastIndex); //输出0
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the use of regular pattern modifiers
Detailed explanation of the use of . sign metacharacters in regular expressions (With code)
The above is the detailed content of Detailed explanation of the use of the g modifier of the regular global matching pattern. For more information, please follow other related articles on the PHP Chinese website!