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