Detailed description of grouping and reverse reference of JavaScript regular expressions, and reverse reference of Regular Expressions
Syntax
Metacharacters (pattern): used for group matching repeatedly
Attribute $1 ~ $9 if it exists, it is used to obtain the matched substring in the corresponding group.
\ 1 or $1 is used to match the content in the first group.
\ 2 or $2 is used to match the content in the first group.
...
\ 9 or $9 is used to match the content in the first group.
Usage example
Var reg =/(A +) (B | C | D) +) (E +)/gi; // This regular expression has four groups // ing relations // RegExp. $1 <-> (A +) // RegExp. $2 <-> (B | C | D) +) // RegExp. $3 <-> (B | C | D) // RegExp. $4 <-> (E +)
The above code also provides $1 ~ $9 usage
$1 ~ $9 is a pre-defined static attribute of a regular expression, which is referenced by RegExp. $1.
Group nesting relationship description
The code above can also describe the nesting relationship of groups.
// Test environment: Chrome browser var str = "ABCDE"; var reg =/(A +) (B | C | D) +) (E +)/gi; str. match (reg); // output: ["ABCDE" reg.exe c (str, 'I'); // output: ["ABCDE", "A", "BCD ", "D", "E"] RegExp. $1; // output: "A" RegExp. $2; // output: "BCD" RegExp. $3; // output: "D" RegExp. $4; // output: "E"
In this way, we can clearly see the nested relationship of the group.
To sum up, when a large group contains a small group, a small group is the group that is placed after the large group, and so on.
Part 2
This section describes the usage of something similar to "\ 1 ".
Reverse reference of group matching
Syntax
Metacharacters \ 1 ~ \ 9: used to indicate a reference to a matched character or group.
Usage example
Reference: [original] AS3 js Regular Expression reverse reference)
There may be some interfaces mentioned above. Here is an example:
// In general, when we want to match any two identical characters (complicated: two identical groups), we can often use the following code: // description: // (\ w) is used to match any character except line breaks and tabs, while \ 1 is a reference to (\ w), so you can understand it as: (\ w) \ 1 is (\ w) // However, the difference between // (\ w) \ 1 and (\ w) is that, (\ w) indicates any two consecutive characters, such as Ac, MM, and K9. // But (\ w) \ 1 can only be AA or CC, 99 is A continuous and identical character // so you can understand that \ 1 is an instantiation reference to (\ w). When (\ w) matches, \ 1 is achieved by Table A. When (\ w) matches 9, \ 1 is expressed as 9. // if so much is said, it may be A bit nonsense, the following example shows how to understand var str = "AA Am 99"; var reg =/(\ w) \ 1/g; str. match (reg); // output: ["AA", "99"]
Therefore, refer to the example of "Regular Expression usage of highlighted keyword search" in the article I referenced above, and I will give a small DEMO of my own improvement.
Although this DEMO does not use any knowledge about reverse references: >_< ::
// Test environment: Chrome browser var key = "keywords"; // search keyword var text = "I am a text, and I have a keywords before this "; // The text to be matched var reg = new RegExp ("(" + key + ")", "g"); text. replace (reg, "<span style = 'color: red'> $1 </span>"); // output: "I am a text, and I have a <span style = 'color: red'> keywords </span> before this"
The following describes reverse references of Regular Expressions in detail.
Example 1:
public static void main(String[] args) { String s="99-3933"; boolean b=Pattern.matches("([\\d])\\1[-]([3])\\1\\2{2}", s); System.out.println(b); }
Reverse reference, matching repeated numbers
([\ D]) ===> \ 1
([3]) ==>\2
Example 2:
public class test { public static void main(String[] args) { String s="99-393399-3933"; boolean b=Pattern.matches("(([\\d])\\2[-]([3])\\2\\3{2})\\1", s); System.out.println(b); } }
Articles you may be interested in:
- Example of reverse expression reference in javascript
- AS3 js Regular Expression backreference)