Role of the g parameter in a javascript regular expression _ javascript skills

Source: Internet
Author: User
After the expression is added with the parameter g, it indicates that global matching can be performed. Note the meaning here. Detailed description:
1) if g is not added to the exec method of the expression object, only the first match is returned, regardless of the number of executions. If g is added, then, the first match is returned for the first execution, and the second match is executed, and so on. For example:

The Code is as follows:


Var regx =/user \ d /;
Var str = "user18duser2dsc ";
Var rs1_regx.exe c (str); // The rs value is {user1}
Var rs21_regx.exe c (str); // The rs value is still {user1}


If regx =/user \ d/g: Then the rs value is {user1}, and the rs2 value is {user2}

This example shows that for the exec method, if the expression is added with g, it does not mean that the exec method can return all the matches, but after g is added, you can get all the matching results in some way. The "method" here is to execute this method for exec.
2) There is no difference between adding g to the test method of the expression object without g.
3) for the match method of the String object, if g is not added, only the first match is returned. If the match method is always executed, the first match is always returned, returns all matches at a time. For example:

The Code is as follows:


Var regx =/user \ d /;
Var str = "user1dge3user2gwe ";
Var rs = str. match (regx); // The rs value is {user1}
Var rs2 = str. match (regx); // The rs2 value is still {user1}


If regx =/user \ d/g, the rs value is {user1, user2}, and rs2 value is also {user1, user2}

4) for the replace method of the string object, if the expression does not add g, only the first match is replaced. If g is added, all matches are replaced.

5) for the split method of the String object, adding g is the same as without g, that is:

The Code is as follows:


Var sep =/user \ d /;
Var array = "user1dfsfuser2dfsf". split (sep );


The array value is {dfsf, dfsf}. When sep =/user \ d/g, the return value is the same.

6) for the search method of the string object, the same is true if g is not added.

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.