Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Dear, hello, everyone. I am “Front-end Xiaoxin”. 😇 has been engaged in front-end development and Android development for a long time


In programming, empty signs are often used for grouping, such as writing long conditional statements. Let’s take a look at the use of parentheses () in regular expressions.

Grouping 01.

Looking at the re in (1) below, we match the character B that appears three times in a row. What if our transformation is, say, abcabcdabcde. What if we match the string ABC that appears three times in a row? Here we need to use the parentheses mentioned in this article. See the re for (2) in the figure below:

02. Branching structure

Parenthes-marked subexpressions also provide support for multiple selection via pipe characters. The difference between the two lines in figure (2) is that the first line has two consecutive abCs and the second line has three consecutive ABCs. We match the first line by adding a group:

03. Use grouping to extract/replace data

Here we use the regular expression mini book v1.1 version of the case (date) related, such as the common date format is YYYY-MM-DD, we use the re to represent, see the following figure:

We are going to add parentheses to prepare for data extraction. Please continue to look at the picture. If the picture is larger, please click the visualization of the fourth quadrant and (2) below:

We then use code to demonstrate the extraction results:

We use Js’s replace function to replaceyyyy/mm/ddIn the form of:

04. Backreference

A backreference here refers to the fact that we can refer to groups that have appeared before the re by identifying them, so it is called a backreference.

The re in the figure below matches the string correctlyThe 2021-10-31 12:20:30and2021/10/31 12:20:30But here’s the surprise2021-10/31 12:20:30Such strings can still be hit. How can we make the symbols of the preceding and following months and days be the same? This is where the backreference from our section is used.

Add a backreference:

Note: remember to be consistent with the previous reference oh, for example:\d{4}(-|\/)\d{2}\1\d{2}[\s]{1}\d{2}(:)\d{2}\2\d{2}. And if you can’t figure out the grouping, you can use the visual page to help analyze it. If the matched grouping does not exist, only the backreferenced character itself will be matched.

05. Non-capture case

We need to use non-capture parentheses (? : p) and (? : p | p | p).

You can see that our original grouping 2 becomes grouping 1, and the original grouping 1 becomes the original parenthesis effect.


Welcome to follow my public account “Front-end Xiaoxin students”, the first time to push original technical articles.

That’s all we have to do with grouping parentheses, so let’s go ahead and copy the re from the previous code and try using the following tool, XDM🤭. The above content is learned from Lao Yao’s regular expression mini-book v1.1 edition, because part of the content is not understood, please read the original more learning.

Recommend your own navigation site: https://it200.cn/

Re writing test site: https://regex101.com/

The regular expression icon is https://jex.im/regulex/