Detailed introduction to grouping and assertion of javascript Regular Expressions

Source: Internet
Author: User
This article mainly introduces the relevant information about javascript Regular Expression grouping and assertion details. For more information about javascript Regular Expression grouping and assertion details, see the following article, for more information, see

Detailed description of javascript Regular Expression grouping and assertions

Tip: reading this article requires a certain regular expression basis.

The assertions in regular expressions appear as advanced applications, not because of their complexity, but because their concepts are abstract and hard to understand. Let's explain them in a simple way today.

Without assertions, the expressions used in the past can only obtain regular strings, rather than irregular strings.

For example, the html source code containsXxxTag, with the previous knowledge, we can only determineAndIs fixed. Therefore, if you want to obtain the page title (xxx), you can only write an expression similar to this:.*In this way, the matching results are complete.XxxTag, not simply the page title xxx.

To solve the above problems, we need to use assertion knowledge.

Before talking about assertions, the reader should first understand the grouping, which helps to understand assertions.

The group is represented by () in the regular expression. According to the understanding of the dish, the group has two functions:

N think of some rules as a group, and then repeat them at the group level to achieve unexpected results.

After grouping n, you can use backward reference to simplify the expression.

First, let's look at the first role. For IP address matching, the simple form can be written as follows:

       \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}

But after careful observation, we can find a certain rule. \ d {1, 3} can be regarded as a whole, that is, they can be regarded as a group, and then this group can be repeated three times. The expression is as follows:

       \d{1,3}(.\d{1,3}){3}

In this way, it is concise.

Let's take a look at the second role and match it.XxxFor tags, simple regular expressions can be written as follows:

       .*

It can be seen that there are two titles in the above expression, which are exactly the same. In fact, you can use group abbreviations. The expression is as follows:

       <(title)>.*
 

This example is actually a practical application of reverse reference. For groups, the entire expression is always counted as 0th groups. In this example, the 0th groups are <(title)> .* And then from left to right, the group number is in sequence. Therefore, the (title) group is 1st.

With the syntax of \ 1, you can reference a group of text content. \ 1 is of course referencing 1st sets of text content. In this way, you can simplify the Regular Expression and write the title only once, put it in the group and reference it later.

Inspired by this, can we simplify the IP address regular expression just now? The original expression is \ d {1, 3 }(. \ d {1, 3}) {3}, where \ d {1, 3} is repeated twice. If backward reference is simplified, the expression is as follows:

       (\d{1,3})(.\1){3}

Put \ d {1st} in a group as (\ d {}), which is a group ,(. \ 1) is a group of 2nd. In the group of 2nd, the group of text content is referenced to the group of 1st through the \ 1 syntax.

After actual tests, we will find that this writing is incorrect. Why?

Dishes have been emphasizing,Backward Reference refers to only text content, not regular expressions!

That is to say, once the content in the group matches successfully, it is referenced backward,The referenced content is the content after the matching is successful. It references the result, not the expression.

Therefore, the expression (\ d {}) (. \ 1) {3} Actually matches four IP addresses with the same number, for example, 123.123.123.123.

So far, readers have mastered the legendary back-to-back reference, which is so simple.

Next, let's talk about assertion.

The so-called assertion indicates the front or back side of a string, and a string meeting a certain rule will appear.

Taking the example at the beginning of the article as an example, we want xxx, Which is irregular, but there will certainly be, There will certainly beThis is enough.

Before xxx is specified, And then use the forward-looking asserted, expression :(? <= <Title>). * </p> <p> after a specified xxx is displayedThe expression is :.*(? =)

The two are combined, that is (? <=).*(? =)

In this way, xxx can be matched.

I believe that the reader is blind to this. Don't worry. Let's talk about it later.

In fact, it is very easy to master the rules, whether it is the first or the last release, it is relative to xxx, that is, relative to the target string.

If there is a condition behind the target string, it can be understood as the target string is in the front, first asserted, placed after the target string.

If there are conditions on the front side of the target string, it can be understood that the target string is placed before the target string, and then the post-development assertions are used.

If a condition is specified, it is positive.

If a condition is not met, it is negative.

Assertion is only a condition. It helps you find the strings you actually need and it does not match!

(? = X)

Assertion with zero width. The matching continues only when the child expression X matches the right side of the position. For example,/w + (? =/D) match the word followed by a number instead of the number. This construction will not be traced back.

(?! X)

Assertion with Zero Width and negative first. The matching continues only when the child expression X does not match the right side of the position. For example,/w + (?! /D) the word that does not match the digit, but does not match the digit.

(? <= X)

Assertion after the width is zero. The matching continues only when the child expression X matches on the left side of the position. For example ,(? <= 19) 99 matches the 99 instance following 19. This construction will not be traced back.

(?

Assertion after negative width. The matching continues only when the child expression X does not match on the left side of the position. For example ,(?

It can be seen from the expression of assertion that it uses a grouping symbol, but a question mark is added at the beginning. This question mark means that this is a non-capturing group, and this group has no number, it cannot be used for backward reference, but can only be used as an assertion.

The above is the detailed introduction of javascript Regular Expression grouping and assertions. For more information, see other related articles in the first PHP community!

Related Article

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.