Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of the g modifier of the regular global matching pattern

php中世界最好的语言
Release: 2018-03-30 13:46:22
Original
2243 people have browsed it

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")
Copy after login

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));
Copy after login

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));
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template