Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the regular expression g modifier

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the regular expression g modifier how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Regular expression g modifier:

The G modifier language specifies that the regular expression performs a global match, that is, the search continues after the first match is found.

Grammatical structure:

Constructor mode:

New RegExp ("regexp", "g")

Object direct quantity method:

/ regexp/g

Browsers support:

IE browsers support this metacharacter.

Firefox browser supports this metacharacter.

Google browser 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 match, and after the first match is successful, the match will not continue.

Example 2:

Var str= "this is an antzone good"; var reg=/an/g;console.log (str.match (reg))

The above code matches to two "an".

The following is a supplement

This article details the global matching pattern / g usage of regular expressions in js, the code is as follows:

Var str = "123#abc"; var re = / abc/ig; console.log (re.test (str)); / / output ture console.log (re.test (str)); / / output false console.log (re.test (str)); / / output ture console.log (re.test (str)); / / Export false

If you use the "g" identifier when creating a regular expression object or set its global attribute to ture, the newly created regular expression object will use the pattern to globally match the string to be matched. Multiple matches can be performed on a specified string to find in global match mode. Each match uses the value of the lastIndex property of the current regular object as the starting position to start the search in the target string. The initial value of the lastIndex property is 0. After the 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, which is used to identify the location to start the search the next time the match is performed. If no matching item is found, the value of lastIndex will be set to 0. When the global match flag for a regular object is not set, the value of the lastIndex property is always 0, and each match is performed to find only the first matching item in the string. You can use the following code to view the values that match the corresponding lastIndex attribute during execution, as follows:

Var str = "123#abc"; var re = / abc/ig; console.log (re.test (str)); / / output ture console.log (re.lastIndex); / / output 7 console.log (re.test (str)); / / output false console.log (re.lastIndex); / / output 0 console.log (re.test (str)); / / output ture console.log (re.lastIndex); / / output 7 console.log (re.test (str)) / / output false console.log (re.lastIndex); / / output 0 Thank you for reading this article carefully. I hope the article "how to use the regular expression g modifier" shared by the editor will be helpful to you. At the same time, I hope you will support it and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report