# Summary of regular expression g,m parameters __ Regular Expressions

Source: Internet
Author: User
A summary of the g,m parameters of regular expressions
In order to answer the meaning of "/g" in "Regular expression (/[^0-9]/g,"). "The question, and in order to be able to have a more comprehensive and profound understanding of regular expressions, I will summarize some of the key points and confusing places."

Summary 1: Use of attachment parameter G

After the expression plus the parameter G, it indicates that a global match can be made, noting the meaning of "can" here. We describe in detail:

1) for the Exec method of an expression object, without adding g, only the first match is returned, regardless of how many times it is executed, and if you join G, the first match is returned, and then the second match is returned, and so on. For example
var regx=/user\d/;
var str= "USER18DSDFUSER2DSFSD";
var rs=regx.exec (str);//At this time the value of Rs is {user1}
var rs2=regx.exec (str);//At this time the value of Rs is still {user1}
If regx=/user\d/g, then the value of the RS is {user1},rs2 = {User2}
This example shows that for the Exec method, the expression joins G, not that the Exec method can return all matches, but that after adding g, I can somehow get all the matches, the "way" for exec, which is the way to execute this method in turn.

2) for the test method of an Expression object, adding G is no different than G.

3) for the match method of the string object, not to join G, but also to return the first match, always execute the match method will return the first match, join G, return all matches at once (note that this is different from the Exec method of the expression object, for Exec, The expression does not return all matches at once, even if you add G. For example:
var regx=/user\d/;
var str= "USER1SDFSFFUSER2DFSDF";
var rs=str.match (REGX);//At this time the value of Rs is {user1}
var rs2=str.match (REGX);//At this time the value of Rs is still {user1}
If regx=/user\d/g, then the value of Rs {User1,user2},rs2 is also {user1,user2}

4) for the Replace method of a String object, the expression does not join G, then only the first match is substituted, and if G is added, all matches are replaced. (The beginning of the three test questions can be very good to illustrate this point)

5) for the split method of a String object, plus g is the same as no G, i.e.:
var sep=/user\d/;
var array= "USER1DFSFUSER2DFSF". Split (Sep);
The value of the array is {DFSF, DFSF}
At this point, the return value is the same sep=/user\d/g.

6) for the search method of the string object, the same is true with the addition of G.

Summary 2: Use of additional parameter m

Additional parameter m, indicating that multiple lines can be matched, but this only works when using the ^ and \$ patterns, and in other modes, multiple lines can be matched without adding m (in fact, multiple lines of string is also a normal string), we illustrate this

1) Use the example of ^
var regx=/^b./g;
var str= "Bd76 Dfsdf
Sdfsdfs dffs
B76DSF SDFSDF ";
var rs=str.match (REGX);
Adding g and not joining G at this time will return only the first match {BD}, if regx=/^b./gm, return all matches {bd,b7}, and note that if regx=/^b./m, only the first match will be returned. So, adding m indicates that multiple lines can be matched, and adding g indicates that a global match can be made, which together is a multi-line global match.

2) Examples of using other modes, such as
var regx=/user\d/;
var str= "Sdfsfsdfsdf
Sdfsuser3 dffs
B76DSF User6 ";
var rs=str.match (REGX);
Without the parameter G at this time, return {User3}, add parameter G to return {USER3,USER6}, add not to M does not affect this.

3) So for m we need to be clear about its use, remembering that it only works for the ^ and \$ patterns, in which the function of M is: if you do not join M, you can only match in the first row, and if you join M, you will be able to match 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 point, the value of RS is null, if the value added to G,rs is still null, if added m, then the value of RS is {BJ} (that is, there is no match in the first row, because there is a parameter m, so you can continue to go to the following line to find if there is a match), if M and G are added, (only m does not add G description, can go to multiple lines to match, but found a match to return, adding g to indicate that all the matches in multiple rows returned, of course, for the match method, for the exec, you need to execute several times to return)

Summary 3: In the HTML textarea input field, press a enter key, the corresponding control character is "\ r \ n", that is, "carriage return", instead of "\n\r", that is, "line break carriage return", we look at a previous example we have mentioned:
var regx=/a\r\nbc/;
var str= "A
BC ";
var rs=regx.exec (str);

Result: The match was successful, the value of Rs was: {}, if the expression is/a\n\rbc/, it will not be matched, so a "enter" key in the general editor represents "carriage return" instead of "line break", at least in the textarea domain.

Related Keywords:

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.