JavaScript regular expression (2) use skills

Time:2021-4-12

 1、 Character class

Concept:Put individual direct character into square brackets to form character class.

    Note:Red is not particularly clear

[…] – indicates that any character in the character class satisfies the     

[^…] – except for any character in the character class

      … any character other than the newline (\ n) and Unicode terminator

\W – (word) any ASCII character, = = [a-za-z0-9]

\W — any non ASCII character, = = [^ a-za-z0-9]

\S – (space) any Unicode white space character

\S — any non Unicode white space character

\D — any ASCII number, equivalent to [0-9]

\D — except for ASCII numbers, = = [^ 0-9]

[[b] – backspace direct quantityWhat’s the use

2、 Repeat

      It is repeated that: {n, m}, {n,}, {n},?, +, * are not explained one by one

    Note: repetition is the first mock exam of one or more copies.

3、 Non greedy repetition (see is to understand a little, do not understand after the experiment!)  

Copy codeThe code is as follows:
var s = “aaaaabab”;
var tanlan = /a*b/;
var landu = /a*?b/;
Var t = s.match (tanlan); / / it is supposed to be t: aaaaabab, l: aaaaab
Var L = s.match (Landu); / / the two matched arrays are the same

4、 Select, group, and reference

|: Select

Expressions with matching symbols from left to right

Var regchoose = / – D {2} | – w {4} /; / / either two decimal places or four lowercase letters. If you match the first, you don’t match the second

(): combination

Function 1: combine individual items into subexpressions

Var regcombination = / (rookies eat insects) + insects eat vegetables / / / “rookies eat insects” must appear once or more

Function 2: take out the string and match it later