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.