What does "G" mean in regular expressions the use of attachment parameter G _ Regular expression

Source: Internet
Author: User
Tags regular expression first row

What do you mean by "/g" in the "Regular expression (/[^0-9]/g,")? "This question, also in order to be able to have a more comprehensive and profound understanding to the regular expression, I will summarize some key points and the easily confused place again."

Summary 1: Usage of appendix Parameter G

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

1 for the Exec method of the expression object, do not join G, return only the first match, no matter how many times the execution is so, if G is added, then the first execution returns the first match, then executes the second match, 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 RS is {User1},rs2 value is {user2}
This example illustrates: for the Exec method, the expression adds G, not that the Exec method can return all matches, but that after G, I can get all the matches in some way, and the "way" for exec is to execute this method sequentially.

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

3 for the match method of a string object, do not join G, just return the first match, the match method always returns the first match, joins G, returns all matches at a time (note that this is different from the Exec method of an 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 is {User1,user2},rs2 value is also {User1,user2}

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

5 for the string object's Split method, plus G and no g are the same, namely:

var sep=/user\d/; 
var array= "USER1DFSFUSER2DFSF". Split (Sep);

The value of the array is {DFSF, DFSF}
At this point sep=/user\d/g, the return value is the same.

6 for a String object search method, plus g is the same.

Summary 2: Use of additional parameter m

Additional parameter m, which indicates that multiple rows can be matched, but this only works when using the ^ and $ schemas, and in other modes, adding no m can do multiple rows matching (in fact, the string is also a normal string), we illustrate this point

1 Examples of using ^

var regx=/^b./g; 
var str= "bd76 dfsdf 
sdfsdfs dffs b76dsf 
"; 
var rs=str.match (REGX);

When G is added and no g is added, only the first match {BD} is returned, and if regx=/^b./gm, all matches {bd,b7} are returned, noting that if regx=/^b./m, only the first match is returned. Therefore, adding m indicates that multiple rows can be matched, and G indicates that global matching can be made, and that a combination of multiple lines of global matching is possible

2 use examples of other patterns, such as

var regx=/user\d/; 
var str= "Sdfsfsdfsdf 
sdfsuser3 dffs 
b76dsf user6"; 
var rs=str.match (REGX);

At this time, no parameter g, then return {User3}, add parameter G return {USER3,USER6}, plus does not add m does not affect this.

3 So for M we have to be clear about its use, and remember that it works only on the ^ and $ patterns, and in both modes, the action of M is: if you don't join M, you can only match in the first row, and if you add M you can match all the rows. Let's look at one more example of ^

var regx=/^b./; 
var str= "ret76 dfsdf 
bjfsdfs dffs b76dsf 
"; 
var rs=str.match (REGX);

At this point, the value of RS is null, and if the value added to G,rs is still null, if M is added, the value of Rs is {BJ} (that is, the match is not found in the first row, because there is a parameter m, you can continue to go to the following line to find if there is a match) and if both M and G are added, then return {BJ,B7} (add only m without G description, you can go to multiple lines to match, but find a match to return, add G to indicate that all the matches in multiple rows back, of course, for the match method is so, for exec, you need to perform several times in order 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 line", instead of "\n\r", that is, "line carriage return", we look at a previous example we have cited:

var regx=/a\r\nbc/; 
var str= "a 
BC"; 
var rs=regx.exec (str);

Results: Match succeeded, the value of Rs is: {}, if the expression is/a\n\rbc/, then will not be matched, so in the general editor a "enter" key represents "carriage return line" instead of "newline carriage return", 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.