JavaScript regular expressions in detail group matching and back reference

  • 2021-01-25 07:03:24
  • OfStack

grammar

Metacharacters: (pattern) Function: Groups used for repeated matching

Attribute $1~$9 if it (they) exists to get the substring matched in the corresponding group

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

\2 or $2 is used 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 A grouping 
// Corresponding relations between 
//RegExp.$1 <-> (A+)
//RegExp.$2 <-> ((B|C|D)+)
//RegExp.$3 <-> (B|C|D)
//RegExp.$4 <-> (E+) 

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

$1~$9 are static properties predefined by the regular expression and referenced by RegExp.$1

Grouping nested relationship descriptions

The above code can also illustrate the nesting relationship of groups


// The test environment  Chrome The 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 easy to see the nesting of the groups

To summarize: when there is a small group in a large group, the small group is the group next to the large group, and so on

Part 2

This section mainly explains the use of something like "\1"

Backreferences for grouping matches

grammar

Metacharacters \1~\9 function: Used to represent a reference to a previous character or group to be matched

Usage examples

AS3 js regular expression backreference (backreference)

This may be a mouthful, but here's an example:


//1 Generally, when we want to match any two characters of the same character ( complex 1 A point is two identical groups ) when , It is often written in the following way 
// Description: 
//(\w) Used to match any character except newline and TAB characters ,  while \1 Is the (\w) the 1 A reference ,  So you can think of it as : (\w)\1  is (\w)(\w)
// but ,
//(\w)\1  and  (\w)(\w) Here's the difference , (\w)(\w) Represents any two consecutive characters ,  Such as Ac, MM, K9,  Can be ,
//  but (\w)\1 Can only be AA, CC, 99  In this way, the same characters are consecutive 
// so ,  You can think of it this way , \1  Is the (\w) the 1 There are three instantiation references ,  when (\w)  Match to the A when , \1  Are expressed by the A,  when (\w) matching 9 when , \1  Be represented as 9
// So much for that ,  Maybe a little bit of crap. ,  Here's an example that makes sense 
var str = "AA Am 99";
var reg = /(\w)\1/g;
str.match(reg);// The output : ["AA", "99"] 

So, following the example of "keyword search highlighted regular expression usage" from the article I cited above, I present my own little version of DEMO

Although DEMO does not use any knowledge about back references: > _ < ::


// The test environment  Chrome The browser 
var key = "keywords";// Search for keywords 
var text = " I am a text, and I have a keywords before this";// Text to be matched 
var reg = new RegExp("("+key+")","g");
text.replace(reg,"<span style='color:red'>$1</span>");// The output : " I am a text, and I have a <span style='color:red'>keywords</span> before this"

Let's take a closer look at regular expression backreferences

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); 
}

Backreference to match duplicate 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); 
 } 
}

Related articles: