On JavaScript regular expression grouping matching

Source: Internet
Author: User
Tags regular expression


Metacharacters: (pattern) function: grouping for repeated matches

Attribute $1~$9 If it exists, it is used to get the substring matched in the corresponding packet

1 or to match the contents of the first group

2 or $ is used to match the contents of the first group


9 or $ is used to match the contents of the first group

Usage examples

1 2 3 4 5 6 var reg =/(A +) (b| c| D) +) (e+)/gi;//The regular expression has 4 groupings//Correspondence//regexp.$1 <-> (A +)//regexp.$2 <-> (b| c| D) +//regexp.$3 <-> (b| c| D)//regexp.$4 <-> (e+)

The code above also gives the use of $1~$9

$1~$9 is a predefined static property of a regular expression, referenced by regexp.$1

Grouping nested Relationship Description

The preceding code can also describe nested relationships for grouping

1 2 3 4 5 6 7 8 9 Test environment Chrome browser var str = "ABCDE"; var reg =/(A +) (b| c| D) +) (e+)/gi; Str.match (reg);//output: ["ABCDE"] reg.exec (str, ' I ');//output: ["ABCDE", "A", "BCD", "D", "E"] regexp.$1;//output: "A" regexp.$2;// Output: "BCD" regexp.$3;//output: "D" regexp.$4;//output: "E"

This makes it clear that the nested relationships of the grouped

In summary: When there are small groupings in large groupings, small groupings are grouped behind the large group, and so on

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