For a summary of the regular expression g and M parameters, in order to answer "/g" in "Regular Expression (/[^ 0-9]/g,"), what does it mean ?"

Source: Internet
Author: User

What does "/g" in the regular expression (/[^ 0-9]/g, '') mean ?" This problem also aims to make it easier for you to have a more comprehensive and profound understanding of regular expressions. I will give a systematic summary of some key points and easily confused points.

Conclusion 1: The attachment parameter G usage

After the expression is added with the parameter G, it indicates that global matching can be performed. Note the meaning of "yes" here. Details:

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, the first match is returned for the first execution, and the second match is returned for the execution, and so on. For example
VaR regx =/user \ D /;
VaR STR = "user18dsdfuser2dsfsd ";
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, I can get all the matching results in some way. The "method" here is to execute this method in sequence for exec.

2) adding G to the test method of the expression object without adding G is no difference.

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. If G is added, returns all matches at a time (note that this is different from the exec method of the expression object. For exec, the expression does not return all matches at a time even if G is added ). For example:
VaR regx =/user \ D /;
VaR STR = "user1sdfsffuser2dfsdf ";
VaR rs = Str. Match (regx); // The RS value is {user1}
VaR rs2 = Str. Match (regx); // The RS 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 is not added with G, only the first match is replaced. If G is added, all matches are replaced. (The three test questions at the beginning can illustrate this point)

5) for the split method of the string object, adding G is the same as without g, that is:
VaR Sep =/user \ D /;
VaR array = "user1dfsfuser2dfsf". Split (SEP );
Then the array value is {dfsf, dfsf}
In this case, SEP =/user \ D/g and the return value is the same.

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

Conclusion 2: Use of the additional parameter m

The Parameter m is appended to indicate that multi-row matching can be performed, but this only works when the ^ and $ modes are used. In other modes, you can perform multi-row matching without adding M (the multi-row string is also a common string ).

1) example of using ^
VaR regx =/^ B./g;
VaR STR = "bd76 dfsdf
Sdfsdfs dffs
B76dsf sdfsdf ";
VaR rs = Str. Match (regx );
At this time, if G is added or G is not added, only the first match {BD} is returned. If regx =/^ B. /GM, then all matches {BD, B7} are returned. Note that if regx =/^ B. /m, then only the first match is returned. Therefore, adding M indicates that multi-row matching can be performed, and adding G indicates that global matching can be performed.

2) examples of using other modes, such
VaR regx =/user \ D /;
VaR STR = "sdfsfsdfsdf
Sdfsuser3 dffs
B76dsf user6 ";
VaR rs = Str. Match (regx );
If the parameter G is not added, {user3} is returned. If the parameter G is added, {user3, user6} is returned.

3) For m, we need to know how to use it. Remember that it only works for the ^ and $ modes. In these two modes, the role of M is: If M is not added, then the matching can only be performed on the first row. If M is added, the matching can be performed on all rows. Let's look at another ^ example.
VaR regx =/^ B ./;
VaR STR = "ret76 dfsdf
Bjfsdfs dffs
B76dsf sdfsdf ";
VaR rs = Str. Match (regx );
At this time, the RS value is null. If G is added, the RS value is still null. If M is added, the RS value is {BJ} (that is, the match is not found in the first line. Because the Parameter M exists, you can continue to find the following row to see if there is a match.) If both M and G are added, {BJ is returned, b7} (only adding m without g description, you can go to multiple rows for matching, but after finding a match, return. Adding G indicates that all the matches in multiple rows are returned, of course, this is true for the match method. For exec, it must be executed multiple times to return results in sequence)

Conclusion 3: In the textarea input field of HTML, press the Enter key and the corresponding control character is "\ r \ n", that is, "carriage return ", instead of "\ n \ r", that is, "line feed and carriage return", let's look at an example we mentioned earlier:
VaR regx =/A \ r \ NBC /;
VaR STR ="
BC ";
VaR rsw.regx.exe C (STR );
Result: The matching is successful. The RS value is: {}. If the expression is/A \ n \ rb/, it will not be matched, therefore, in the General Editor, a "enter" key represents "Carriage Return", rather than "line feed", at least in the textarea domain.

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.