The grouping and forward-looking matching of JavaScript regular expressions

Source: Internet
Author: User

This article mainly explains the group match and the forward match in the regular expression of JavaScript, need to have the basic understanding to the regular, I have been to two kinds of match ambiguity unclear. So summarize here, if there is not, but also hope the big God pointing.

1. Group matching:
1.1 Capturing group matching ()
2.2 Non-capturing group matching (?:)
2 forward-looking matches:
2.1 Forward forward matching: (? = expression) must be followed by a match.
2.2 Reverse forward match: (?! expression) must not have anything behind it.

1.1. Capturing group Matching ()

varSTR1 ="Holle Word 123456 can 12s a 123 a";varREG1 =/([a-z]+) \s (\d+)/;//not the global schema, grouped by (), there are two groups, each of which will be matchedvarREGG1 =/([a-z]+) \s (\d+)/g;//Global mode G, grouped by (), there are two groups, each group will match//Res: Non-global modeConsole.log (Reg1.exec (str1));//exec () method: ["Wold 123456", "word", "123456"]Console.log (Str1.match (REG1));//Match () method: ["Word 123456", "word", "123456"]Console.log (regexp.$1);//Gets the result that matches the first grouping ([a-z]+): WordConsole.log (regexp.$2);//Gets the result that matches the first grouping (\d+): 123456//Res: Global modeConsole.log (Regg1.exec (str1));//exec () method: ["Wold 123456", "word", "123456"]Console.log (Str1.match (REGG1));//Match () method: ["Word 123456", "Can", "a 123"]Console.log (regexp.$1);//Gets the result that matches the first grouping ([a-z]+): aConsole.log (regexp.$2);//Gets the result that matches the first grouping (\d+): 123

Analysis: This regular expression matches at least one letter, followed by a space, and then at least one number,

Non-Global is the first match is correct and will not match again,

The value extracted by the 1.exec () method is specified, the first value is the text that matches the regular expression, such as "/([a-z]+) \s (\d+)/" on the example above), the 2nd value is the first character subexpression (that is, the first grouping), as in the example above ([A-z]), and so on


2. Even though the global mode, exec () does not globally match, the Loop call exec () is the only way to globally match, so you will find that the result of using the Exec () method above is the same


3. While the match method matches the capturing grouping of the global pattern, it will match the regular expression globally, but will not match the subexpression (grouping), so you will find that the result of Str1.match (REGG1) above is not individually grouped ([a-z]+) letters or groupings (\d+) The number appears, but the global match is the entire regular, so the result is ["Word 123456", "Can", "a 123"]


The 4.match method will match the regular expression globally and will match the subexpression (grouping) in the capture grouping match of the non-global pattern, so you find that the Str1.match (REG1) match has a separate grouping of matches, but because it is non-global, the first match is done correctly. Only ["Wold 123456", "word", "123456"], "Wold 123456" is the result of the entire expression match, "word" is the result of the first grouping ([a-z]+) match, "123456" is the result of the second grouping (\d+) match


5.$1,$2 ... Contains the corresponding inverse reference in the regular expression, in global and non-global mode, if the result set has multiple, will be calculated with the result of the last match, such as above, global mode, matching a total of three matches, ["Word 123456", "Can", "a 123"], then the last "A 123" is the result of all the groupings, the first grouping is ([a-z]+) matches the letter so is a, the second group is a number (\d+), so is 123, and so on, if only appear once, once is also when the last time, the nature is the same analysis, hahaha, a bit superfluous ....

1.2 (?:) non-capturing grouping matches, not capturing sub-expressions (grouping)

varSTR1 ="Holle Word 123456 can 12s a 123 a";varREG2 =/(?: [a-z]+) \s (?: \ d+)/;varREGG2 =/(?: [a-z]+) \s (?: \ d+)/G;//Res: Non-global modeConsole.log (Reg2.exec (str1));//exec (): direct match ["Wold 123456"],Console.log (Str1.match (REG2));//Match () method: ["Word 123456"]//Res: Global modeConsole.log (Regg2.exec (str1));//exec (): direct match ["Wold 123456"],Console.log (Str1.match (REGG2));//Match () method: ["Word 123456", "Can", "a 123"]

Analysis, and the above capture grouping match is the same as parsing, just no longer matches sub-expressions (grouping)

2.1 Forward forward matching: (? = expression) must be followed by a match.

Note: The Forward grouping match (? = expression) is used as a match and is not returned as a matching result

// instance, extract the picture name in JPG type var " ab.jpg,admin/12.gif,and.jpg " ; var reg3 =/[^\\]\w+ (? =\.jpg)/G;console.log (Str2.match (REG3)); // ["AB", ", and"]

2.2 Reverse forward match: (?! expression) must not have anything behind it.

// Example: match a continuous a letter of more than three and cannot have a number behind it var  " Aaa12345,aaaadmin,aaaaaadd,dlala " ; var  REG4 =/a{3,} (?! \d+)/G;console.log (Str3.match (REG4)); // ["AAAA", "aaaaaa"]

End, thank you ....

The grouping and forward-looking matching of JavaScript regular expressions

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.