Regular Expression, inert match mode (?), Regular Expression inertia
Regular Expression:
In the greedy match pattern section, we have already said that human nature is greedy, and we hope to get more money, status, and even beautiful women, but there are also many people who have little desires, as long as the basic needs of life can be met, there are also such matching principles in regular expressions, the following will be an introduction.
I. Concept of inertia mode:
This mode is the opposite of greedy mode. It matches as few characters as possible to satisfy the regular expression. For example:
var str="axxyyzbdkb"; console.log(str.match(/a.*b/));
The above code is greedy mode, so it can match the entire string, and then modify it to the inert match mode:
var str="axxyyzbdkb"; console.log(str.match(/a.*?b/));
The above code is an inert match. The method is to add a question mark (?) after the repeated quantifiers (?) You can.
The inert match mode is to match as few characters as possible, but must meet the matching rules of regular expressions, such as the above Code, * The regular expression can be repeated with zero or more characters or subexpressions, but the end of the regular expression must be B. Therefore, the regular expression can match the axxyyzb in the above string.
Summary:
1. Add a question mark (?) after the repeated quantifiers (?) To form an inert match.
2. The inert match matches as few characters as possible, but the entire match mode must be satisfied.
Ii. List of inertia delimiters:
Syntax structure |
Semantic explanation |
*? |
You can repeat any time, but repeat as few times as possible. |
+? |
You can repeat once or any number of times, but try to repeat as few times as possible, but the minimum number of times is 1. |
?? |
It can be repeated 0 times or 1 time, but as few as possible. |
{N, m }? |
You can repeat n to m, but as few as possible. The minimum number of matches is n. |
{N ,}? |
It can be repeated more than n times, but as few as possible, at least match n. |
First introduce a more detailed website
Http://www.bkjia.com/article/31491.htm
Next is my profile
In fact, greed and inertia are easy to understand. From the literal meaning, we can know that the so-called "greedy" means that if it meets the requirements, it will keep matching until it cannot be matched, this is the greedy mode. The so-called inertia pattern ends when it is matched to a suitable pattern and does not continue to be matched. Here are several examples to illustrate.
First, let's talk about the greedy pattern identifier: + ,?, *, {N}, {n ,}, {n, m}. inert mode: + ?,??, *??, {N }?, {N ,}?, {N, m }?;
Example 1
Var pattern =/8 [a-zA-Z0-9] * 7/; greedy mode var string = "abc8defghij7klngon8qrstwxy7 ";
The greedy mode * is used to indicate that there can be any number of letters between 8 and 8. Then, this regular expression matches the first 8 first, there is no limit to match the following content, as long as the following content are satisfied with the [a-zA-Z0-9] can be. Keep matching until the match is no longer available. Check whether the next one is 7. If not, move forward to another one (spit out one to see if it is 7 ), if you do not continue to vomit until 7 is spit out, then the Matching content is the content between them. Therefore, the content matched by the result is the entire string.
Varpattern =/8 [a-zA-Z0-9] *? 7/ig; inert mode var string = "abc8defghij7klngon8qrstwxy7 ";
The above regular expression uses the inert mode *?, At this point the matching method is like this, first match an 8, then in the back to match a character to see if it meets the [a-zA-Z0-9], if it matches, then to see followed by a character is not 7, if it is 7, it will end, if not, then match a character, to see if it meets the [a-zA-Z0-9], if it matches, then look at the next character is not 7, if it is 7, it will end. Otherwise, it will be cyclically followed by the above method, so far as the guidance is met.
(2) greedy and inert patterns can also be expressed in another way.
Example 2
var test="]*\/>/ig;
In this way, the inertia mode can also be implemented. [^>] indicates that cannot appear>, so you can find each tag in the result.