On JavaScript regular expression grouping matching

Source: Internet
Author: User
Tags regular expression

Grammar

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

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.