Deep understanding of JS Regular expression---grouping
Before writing an article on the beginner beginners, this thought to the regular expression relatively better understanding but today I met a pit, may be not careful why they are, today's focus and you share the JavaScript regular expression in the grouping. If you do not understand the JS regular expression can click here to learn more.
In the regular use of the group is still relatively wide, I understand the grouping is a pair of parentheses (), each pair of parentheses represents a grouping, grouping can be divided into:
• Capturing groupings
• Non-capture grouping
Capturing grouping
A capturing group is the result of grouping in a function such as match exec, in the form of the second and third items. Let's take a look at an example.
var reg =/test (\d+)/;
var str = ' new test001 test002 ';
Console.log (Str.match (reg));//["test001", "001", Index:4, Input: "New test001 test002"]
In code (\d+) is a grouping (some people also call him a child mode), but it all means the same thing, in the example above test001 is the result of an exact match, but the grouping match is looking for characters that match the child pattern \d+ from the entire matching result (i.e. test001). This is obviously 001. But this is what happened today.
var reg =/test (\d) +/;
var str = ' new test001 test002 ';
Console.log (Str.match (reg));//["test001", "1", Index:4, Input: "New test001 test002"]
The difference is (\d+) changed to (\d) +, the whole match result or test001 but the result of the first group match is different. Let's take a slow analysis of their differences.
(\d+) This whole is a grouping of cases, because the match pattern is greedy by default, that is, as much as possible to match all \d+ match to the result is 001 and then outside add a pair of parentheses is a grouping, so that the result of the first group match is 001. Let's look at the second example ( \d) + Also This is also a greedy pattern that first matches the 0 followed by the 0, and it matches to the end of 1, which also matches to the end of the match. It looks like the match in the first example doesn't make any difference. But here's a grouping (\d) that matches a single number, as I understood it before 0 but this understanding is wrong. Because the whole match is greedy, as much as possible to match the group (\d) will capture the last match to the result of 1, if the right and wrong greedy mode that will be as little as possible to match
var reg =/test (\d) +?/;
var str = ' new test001 test002 ';
Console.log (Str.match (reg));//["test001", "0", Index:4, Input: "New test001 test002"]
This (\d) match result is 0, although there are results to match, but here is as little as possible to match
Non-capturing grouping
Non-capture grouping that's where you need a pair of parentheses, but you don't want him to be a catch group, which means that you don't want this grouping to be captured by functions like MACTH exec, usually in front of parentheses: that is (?:p Attern) This becomes a non-capture grouping,
var reg =/test (?: \ d) +/;
var str = ' new test001 test002 ';
Console.log (Str.match (reg));//["test001", Index:4, Input: "New test001 test002"]
In this case, the result of match will not be the content of the packet matching, which is less than 1 of the second item.
This article focuses on the difference between (\d+) and (\d) +, is also the pit I stepped on today, if there is a mistake, please correct me.
The above in-depth understanding of JS Regular Expression---grouping is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.