Regular Expression, inert match mode (?), Regular Expression inertia

Source: Internet
Author: User

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.

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.