The purpose of the parentheses in the regular () is detailed

Source: Internet
Author: User
Tags regular expression

The parentheses in the regular expression function to group the characters and save the matching text. Today we are going to discuss in detail the use of the parentheses () in the next regular round, and I hope we can help you with your study.

First, as we mentioned earlier, if the second parameter in the Replace () method is a callback function, then the parameter in the callback function is the result of the match success .... , do you think there are multiple parameters in the callback function?

Let me give you an example:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 <! doctype>

The above example requires you to look at the value of A1,A2,A3 separately, and you will find some regularity

View a1:2015-8-individually

View a2:2015 8 individually

View a3:-individually-

Three parameters of the image of an analogy, A1 equivalent to the mother, A2 is the older son, A3 is the youngest son, each of the sons have inherited a part of the mother's genes

A1 is Var re=/d+-/g; The results of this regular

A2 is Var re=/(d+) (-)/g; The result of the first bracket on the left

A2 is Var re=/(d+) (-)/g; The result of the second parenthesis on the left

With the use of so many parameters, let's implement the output below: 2015.8.7

Method one: to the son operation


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <! doctype>

Method Two: to the mother operation


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <! doctype>

The relationship between the multiple parameters of the callback function of the replace () method in the regular and the parentheses is found to be complicated.

What is the difference between the bracket [] of the regular expression and the parentheses ()?

The basic meaning: the parenthesis is the parentheses as a whole, the bracket is to match one of the parentheses, the braces are matched several times

But there are different meanings in parentheses and other characters. Detailed Introduction For example:


n is a non-negative integer. Match the determined n times. For example, "o{2}" cannot match "O" in "Bob", but can match two o in "food".


n is a non-negative integer. At least match n times. For example, "o{2,}" cannot match "O" in "Bob", but can match all o in "Foooood". O{1,} "is equivalent to" o+ ". O{0,} "is equivalent to" o* ".


M and n are non-negative integers, where n

[] meaningless, generally used to match a set

Like what

[0-9]: Find any number from 0 to 9.

[A-Z]: Find any character that writes a to uppercase Z from small.

() also matches a collection, but it can also be used as a reverse reference.

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.