Grouping matching of JavaScript regular expressions

Source: Internet
Author: User

Grammar

Metacharacters: (pattern) Action: grouping for repeated matches

Property $1~$9 If it exists, it is used to get the substring matching in the corresponding group

\1 or $ $ to match the contents of the first group

\2 or $ to match the contents of the first group

...

\9 or $9 to match the contents of the first group

Usage examples

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

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

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

Description of the grouping nesting relationship

The above code can also describe nested relationships grouped

// 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"

So you can clearly see the nested relationships of the groupings.

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

Grouping matching of JavaScript regular expressions

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.