What does "g" in a regular expression mean by the use of the attachment parameter g?

Source: Internet
Author: User

What does "g" in a regular expression mean by the use of the attachment parameter g?

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 ); // at this time, 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=“a bc”; var rs=regx.exec(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.


What does the regular expression mean by an asterisk (*)?

* Indicates all, that is, everything can be matched.
Sed '/^ * $/d' test

^ Indicates the beginning,
$ Indicates the end,
The two are combined '/^ * $/d', indicating that all are in the range.
''Indicates defining the range for your reference.

^ * $ Indicates all strings of the Year, including null
 
What is \ 1 and \ 2 in a regular expression? Can you tell me the specific usage?

If the search content is
News1.xxx.com/(\ d +), (\ d00000000.html
In this case, replace \ 1 in it to indicate the Matching content in the first bracket (in this example, it is a string of numbers). The same principle \ 2 indicates the Matching content in the second bracket.

Now you probably have understood: \ 1, \ 2, and so on, to find the matching content corresponding to each bracket, that is, the specific searched content.

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.