Home > Web Front-end > JS Tutorial > body text

Detailed explanation of regular expression grouping (with code)

php中世界最好的语言
Release: 2018-03-30 11:39:34
Original
2011 people have browsed it

This time I will bring you a detailed explanation of regular expression grouping (with code), what are the precautions for regular expression grouping, the following is a practical case, let’s take a look one time.

Regular expression grouping:
If you want to match 3 numbers, the regular expression can be written as follows:

\d{3}

The above code uses repeated quantifiers to match 3-digit numbers.
But in actual applications, it is often necessary to repeat multiple characters. For example, if I want to repeat two characters ab, it is not appropriate to use the following code. The code is as follows:

ab{3}

The above regular expression can only be repeated 3 times b.
In order to repeat multiple characters, you can use parentheses to specify subexpressions or groups to achieve this function, for example:

(ab){3}

The above regular pattern can be repeated ab.
The so-called grouping is to use parentheses to include some items to make them independent logical domains. Then the contents of the parentheses can be processed like an independent unit.

Look at a code example below:

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

The above is a simple ip matching regular expression. The content enclosed by parentheses will be operated as an independent logical field.
The role of grouping:
In regular expressions, grouping plays a decisive role. Here is a brief introduction to its function.

1. Play the original grouping role:

Group individual items to synthesize subexpressions, so that they can be processed like a single character , apply |, +, * or ? and other metacharacters to operate them.
The example code is as follows:

var str="I love javascript and java";
console.log(str.match(/java(script)?/gi));
Copy after login

It can be seen from the running results of the above code that regular expressions can match both stringsjavascript and java. This is because in regular expressions Grouping is used in the formula and repeated quantifiers are used? , can make the previous subexpression repeat 0 or 1 times.

2. Define sub-patterns:

Another very important role of grouping is to define sub-patterns in the complete pattern.
When a regular expression successfully matches the target string, the part that matches the expression in parentheses can be extracted from the target string.
If we match a string that starts with a number and is followed by one or more case-insensitive letters, the regular expression can be written like this:

/\d[a -zA-Z]+/

If what we really care about and need is the number at the beginning, then we can put the number part of the regular expression into parentheses to retrieve the match Extract numbers.

/(\d)[a-zA-Z]+/

##3. Quote subexpression:In regular expression syntax, you can reference the previous subexpression at the back of the same expression. This is accomplished by following a number after "\". This number specifies the position of the parenthesized expression in the regular expression. For example, \1 is the first subexpression with parentheses quoted, and naturally \2 is the second subexpression with parentheses. .
Special note: Since subexpressions can be nested into each other, when calculating the position of the subexpression, you only need to determine the position of the left bracket. For example:

/(java(script))/

In the above regular expression, the nested sub-representation can be represented by \2.

The reference to a subexpression is not the matching pattern of the reference, but a reference to the matching content of the subexpression. Generally speaking, references to subexpressions are generally used to implement a constraint. See the following code:

/(['"])[^'"]*\1/

Normally, the left and right quotation marks match. If there is a double quotation mark in front of it, then the end must also be a double quotation mark. If there is a single quotation mark in front of it, then the end must also be a single quotation mark, and no other quotes can appear in the middle of the string. Quotation marks, for example:

"Script House welcomes you"

'Script House welcomes you'

Then the above regular expression implements this function. When the previous subexpression matches a single quote, then the following \1 also represents a single quote. If the subexpression represents a matching double quote, then the following \2 means double quotes.
Non-reference grouping:
Grouping will occupy a certain amount of system resources, especially when the regular expression is long, it will reduce the matching speed. Sometimes it is just to set up a group and does not require a reference, so using non-reference type grouping will be a good choice.

/(java(?:script))/

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

How to use regular expressions in JS to replace multiple spaces in a string with one space

Detailed explanation of the efficiency of greedy, non-greedy and backtracking using regular expressions in PHP (with code)

The above is the detailed content of Detailed explanation of regular expression grouping (with code). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template