Regular Expression global match mode (g modifier), regular expression global

Source: Internet
Author: User

Regular Expression global match mode (g modifier), regular expression global

Regular Expression g modifier:

The g modifier specifies that the regular expression performs global match, that is, it will continue searching after the first match is found.

Syntax structure:

Constructor method:

new RegExp("regexp","g")

Method of direct object volume:

/regexp/g

Browser support:

Internet Explorer supports this metacharacter.
Firefox supports this metacharacter.
Google Chrome supports this metacharacter.

Instance code:

Instance 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 no global match is performed, the match will not continue after the first match is successful.

Example 2:

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

The above code can match two "".

Below is a supplement

This article describes the global matching mode/g usage of Regular Expressions in Javascript. 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); // outputs false console. log (re. test (str); // output ture console. log (re. test (str); // output false

When a regular expression object is created with a "g" identifier or its unique global attribute value is set to true, then, the newly created Regular Expression object uses the pattern to perform global match 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 the lastIndex is reset to the position index of the next character in the matching content, which is used to identify the position to start searching for the next matching execution, 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 can be used to view the value of the corresponding lastIndex attribute matching execution. The Code is 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); // outputs false console. log (re. lastIndex); // outputs 0 console. log (re. test (str); // output ture console. log (re. lastIndex); // output 7 console. log (re. test (str); // outputs false console. log (re. lastIndex); // output 0

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.