A detailed explanation of JavaScript regular expression packet matching and reverse reference _javascript skills

Source: Internet
Author: User

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 used 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

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

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

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"

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

Part II

This section mainly explains the use of something similar to "\1"

Reverse reference for grouping matches

Grammar

metacharacters \1~\9 function: Used to represent a reference to a matching character or grouping before

Usage examples

Reference article: [Original]AS3 JS Regular expression Reverse reference (backreference)

The above may be a bit clumsy, here is an example:

General situation, when we want to match any two identical characters (the complexity is two identical groupings), you can often use the following notation
//Description:
//(\w) to match any character except for newline and tab, and \1 is a reference to (\w). So you can understand that: (\w) \1 is (\w) (\w)
//But,
the difference between//(\w) \1 and (\w) (\w) is that (\w) (\w) represents any two consecutive characters, such as AC, MM, K9, all available,
//But (\ W) \1 can only be AA, CC, 99 such continuous identical characters
//So, as you can understand, \1 is an instantiated reference to (\w), when (\w) matches to a, \1 is expressed as a, and when (\w) matches 9 o'clock, \1 is represented as 9
//SO much that May be a bit of nonsense, the following example is very good understanding of
var str = "AA Am";
var reg =/(\w) \1/g;

So, referring to the example of "keyword search highlighting regular expression usage" given in the article I quoted above, I give my own improved little demo

Although this demo does not use any knowledge about the reverse reference point:: >_<::

Test environment Chrome Browser
var key = "keywords";//Search keyword
var text = "I am a text, and I have a keywords before this"; With the text
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 ' color: Red ' >keywords</span> before this '

Here is a detailed description of the reverse reference to the regular expression

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

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.