Summary of JavaScript regular expression

Time:2020-11-25

This paper summarizes the content of JavaScript regular expressions. Table form, clear at a glance. With the help of example graphic explanation.

1、 Metacharacter

Summary of JavaScript regular expression

2、 Antonym character

Summary of JavaScript regular expression

3、 Escape character

Summary of JavaScript regular expression

4、 Repeated matching

Summary of JavaScript regular expression

5、 Group / capture

Summary of JavaScript regular expression

6、 Greed and inertia

Summary of JavaScript regular expression

7、 Modifier

Summary of JavaScript regular expression

example

  • Matches phone numbers that start with 11n, 12n or 1nnn, followed by – 7-8 digits.

Summary of JavaScript regular expression

Recommend a tool to represent regularization graphically
https://regexper.com

The above example is translated into a graphical representation as follows:

Summary of JavaScript regular expression

  • Explain what this regular means

/(?:([^:;\(\[]*):)?(.*)/

first(?:...)?(.*)Look at the two outermost brackets. The first one is a non trapping bracket. The? Sign acts on this bracket. The second bracket is capture type and matches multiple arbitrary characters.

then,([^:;([]*):This is a catch bracket and a character: sign

[^:;([]*Capture type brackets also contain a character group (()) and a * sign. In the character group, multiple characters except; ([are matched)

Summary of JavaScript regular expression

One more

/(^\s*)|(\s*$)/g

Summary of JavaScript regular expression

  • A method of telephone number extraction is realized with JS.

For example: “1852145998, 020-888-999845, test 021 – 85421987, 19865754” and the result should be [1852145998, 020-888-999845, 021 – 85421987, 19865754]

Var STR = 1852145998 020-888-999845 test 021-85421987, 19865754 ";
var reg=/(1\d+)|(0[0-9\s\-]+)/g;
console.log(str.match(reg));

Summary of JavaScript regular expression

Regularization method:

. test (string): returns a Boolean value indicating whether the incoming string matches the regular (starting from where indicated by the lastindex property)

. exec (string): returns an array containing matching results (with two additional attributes: input represents the target string of the search, that is, the parameter passed in to the exec method; index indicates the position of the matching result in the target string) or null (cannot match). Even if the G option is added to the regular, continue the search by calling the method of the same regular object multiple times (each search will continue from where indicated by the lastindex property). If the search is completed, lastindex is automatically set to 0). If the G option is not added, the result is the same several times. If no match is found, null is returned and the lastindex of the regular object is automatically set to 0

Related methods of string object:

String. Match ()
String. Search ()
String. Split ()
String. Replace ()

For more information, please refer to the chapter regexp.