In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the attachment parameter g in the regular expression, which is very detailed and has a certain reference value. Friends who are interested must read it!
In order to answer "what does" / g "mean in the regular expression (/ [^ 0-9] / gheroine')?" This problem, and in order to facilitate you to have a more comprehensive and profound understanding of regular expressions, I will systematically summarize some key points and areas prone to confusion.
Summary 1: usage of attachment parameter g
After adding the parameter g to the expression, it indicates that a global match can be performed, and note the meaning of "can" here. We describe in detail:
1) for the exec method of the expression object, if g is not added, only the first match is returned, no matter how many times it is executed. If g is added, the first match is returned on the first execution, the second match is returned on the first execution, and so on. For example
Var regx=/user; var str= "user18dsdfuser2dsfsd"; var rs=regx.exec (str); / / the value of rs is {user1} var rs2=regx.exec (str); / / the value of rs is still {user1}
If regx=/user\ duser2 g; then the value of rs is {user1} and the value of rs2 is {user2}
Through this example to illustrate: for the exec method, the expression added g, it does not mean that the implementation of the exec method can return all the matches, but that after adding g, I can get all the matches in some way, the "way" for exec, is to execute this method in turn.
2) for the test method of the expression object, there is no difference between adding g and not adding g.
3) for the match method of the String object, do not add g, only return the first match, always execute the match method and always return the first match, add g, then return all matches at once (note that this is different from the exec method of the expression object, for exec, the expression will not return all matches at once even if g is added). For example:
Var regx=/user; var str= "user1sdfsffuser2dfsdf"; var rs=str.match (regx); / / the value of rs is {user1} var rs2=str.match (regx); / / the value of rs is still {user1}
If regx=/user\ dUnig, the value of rs is {user1,user2}, and the value of rs2 is also {user1,user2}
4) for the replace method of the String object, the expression does not add g, then only the first match is replaced, and if g is added, all matches are replaced. (the first three test questions can well illustrate this point.)
5) for the split method of String object, adding g is the same as not adding g, that is:
Var sep=/user\ dbat; var array= "user1dfsfuser2dfsf" .split (sep)
Then the value of array is {dfsf, dfsf}
At this point sep=/user\ dUnig, the return value is the same.
6) for the search method of the String object, it is the same whether or not to add g.
Summary 2: the usage of the additional parameter m
The additional parameter m indicates that multi-line matching can be performed, but this only works when ^ and $patterns are used. In other patterns, multi-line matching can be performed with or without m (in fact, a multi-line string is also an ordinary string). Let's give an example to illustrate this point.
1) examples of using ^
Var regx=/ ^ b. / g; var str= "bd76 dfsdf sdfsdfs dffs b76dsf sdfsdf"; var rs=str.match (regx)
If you add g and no g, only the first match {bd} is returned. If regx=/ ^ b. / gm, all matches {bd,b7} are returned. Note that if regx=/ ^ b. / m, only the first match is returned. Therefore, the addition of m indicates that multi-line matching can be performed, and the addition of g indicates that global matching can be carried out. When combined, multi-line global matching can be carried out.
2) examples of using other patterns, such as
Var regx=/user\ dbat; var str= "sdfsfsdfsdf sdfsuser3 dffs b76dsf user6"; var rs=str.match (regx)
If the parameter g is not added, {user3} is returned, and the parameter g is added to return {user3,user6}. Whether or not to add m has no effect on this.
3) so we need to be clear about the use of m, remembering that it only works on ^ and $patterns, in which the role of m is: if you don't add m, you can only match on the first line. If you add m, you can match on all lines. Let's look at another example of ^.
Var regx=/ ^ b. /; var str= "ret76 dfsdf bjfsdfs dffs b76dsf sdfsdf"; var rs=str.match (regx)
At this time, the value of rs is null, and if you add m, the value of rs is still null. If m is added, the value of rs is {bj} (that is, no match was found in the first line, because there is a parameter m, so you can continue to go to the following line to see if there is a match). If both m and g are added, {bj,b7} is returned (only m without g description, you can go to multiple lines to match, but return when a match is found. Adding g indicates that all matches in multiple rows will be returned. Of course, this is true for the match method, but for exec, it needs to be executed multiple times to return in turn.)
Summary 3: press an enter key in the textarea input field of HTML, and the corresponding control character is "\ r\ n", that is, "enter newline" instead of "\ n\ r", that is, "enter enter". Let's take a look at an example we gave earlier:
Var regx=/a\ r\ nbc/; var str= "a bc"; var rs=regx.exec (str)
Result: if the match succeeds, the value of rs is: {}, and if the expression is / a\ n\ rbc/, it will not be matched, so a "Enter" key in the general editor represents "enter newline" rather than "newline return", at least in the textarea domain.
The above is all the contents of the article "how to use the attachment parameter g in regular expressions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.