This article mainly introduces the use of js Regular Expressions for inertia matching and greedy matching, and analyzes the basic concepts of regular expressions and the use of inertia matching and greedy matching in combination with the instance form, for more information about js regular expressions, see the examples in this article. We will share this with you for your reference. The details are as follows:
Before talking about greedy mode and inertia mode, let's review the JS Regular Expression basics:
Writing basics:
① Double quotation marks are not required. Use // include =>/wehfwue123123/. test () directly ();
② Backslash \ escape =>/\. jpg $/
③ Usage basics:. test (str );
Syntax:
① Anchor
/^ A/=> starts with ""
/\. Jpg $/=> end with ". jpg"
② Character class
[Abc]: a, B, or c
[0-9]: a number
[A-z]: a letter
.: Any character
③ Metacharacters
^: Indicates non in [] and starts with a representation outside [].
\ D: [0-9]
\ S: blank space
\ W: [A-Za-z0-9 _]
\ D: [^ \ d]-non-digit
\ S: Non-blank characters
④ Quantifiers
{M, n}: m to n times
Metacharacters:
*: {0 ,}
? : {0, 1}
+: {1 ,}
Difficulty: greedy mode/Inertia Mode
Greedy mode-match as many as possible when matching is successful
Inertia mode-match as few as possible when the match is successful
Explanation 1: Text and text
The greedy and inert quantifiers in regular expressions can control the expression matching process. Do we know the quantifiers? , *, +, You can specify the number of occurrences of the relevant mode. By default, we use a greedy quantizer. Its Matching process starts from the entire string, if there is no match, remove the last one and check whether it matches. This loop ends until the match or the string is null, for example:
Vars = "abbbaabbbaaabbb1234"; varre1 =/. * bbb/g; // * is the greedy quantizer re1.test (s );
The matching process starts from the entire string:
Re1.test ("abbbaabbbaaabbb1234"); // false, remove the last character 4 and continue re1.test ("abbbaabbbaaabbb123"); // false, remove the last character 3 and continue re1.test ("abbbaabbbaaabbb12"); // false, remove the last character 2 and continue re1.test ("abbbaabbbaaabbb1"); // false, remove the last character 1 and continue re1.test ("abbbaabbbaaabbb"); // true, end
Add one more greedy quantizer? It becomes an inert quantizer. The matching process is the opposite. It starts from the first one. If it does not match, it is added. This loop ends until the end of the string. The above is an example.
Vars = "abbbaabbbaaabbb1234"; varre1 = /.*? Bbb/g ;//*? Is the inert quantizer re1.test (s );
The matching process is as follows:
Re1.test ("a"); // false, add a re1.test ("AB"); // false, and add a re1.test ("abb"); // false, add a re1.test ("abbb"); // true, match, save the result, and re1.test ("a") from the next one; // false, add re1.test ("aa"); // false, and re1.test ("aab"); // false, and re1.test ("aabb"); // false, add a re1.test ("aabbb"); // true, match, save the result, and start from the next
Summary:
The default greedy match is a match from the back to the back. The maximum length of the match. Is an inert match followed by a quantizer? Match starting from the beginning of the string, matching with the minimum length
I hope this article will help you design JavaScript programs.
For more articles on the usage and Analysis of JavaScript Regular Expressions for inert matching and greedy matching, refer to the PHP Chinese website!