Regular expression
Regular expression creation
Var re = /ab+c var re = /ab+c Var re = new RegExp("ab+c", "flags") var re = new RegExp("ab+c", "flags") / / through the string expression to create regular, need to escape character has the following several / / [] \ ^ $|? * + ()Copy the code
Method of expression
exec
Reg. Exec (STR)
/d(b+)d/g.exec(' CDBBDBSBZ ')Copy the code
match
Return [match data, capture data, index, input], usage: reg.match(STR)
/d(b+)d/g.match(' CDBBDBSBZ ')Copy the code
test
Return a Boolean value. Usage: reg.test(STR)
/d(b+)d/g.test(' CDBBDBSBZ 'Copy the code
matchAll TODO
Returns an iterator
search
Search (str.search) returns the matched index, -1 if not matched.
CDBBDBSBZ '. Search (/d(b+)d/gCopy the code
replace
Replace matching data, use: str.replace(reg, replace)
'CDBBDBSBZ '. Replace (/d(b+)d/g,' replace ') output: 'c replace BSBZ 'Copy the code
split
Split data according to specifications, usage: str.split(reg)
Split (/db+d/g) output: ['c', 'BSBZ ']Copy the code
List of special characters
Assertion class
^
: Matches the beginning of the input$
: Matches the end of the input\b
: Matches “word” and “non-word” boundaries\B
: Matches congener boundaries, that is, between “words” and “words”, and between “non-words” and “non-words”x(? =y)
: Predicate forward, matchx
bey
followx(? ! y)
: Forward negate assertion, matchx
Don’t bey
follow(? <=y)x
: Backward assertion, matchx
followy
(? <! y)x
: Backward negate assertion, matchx
Don’t followy
[a-za-z0-9_], otherwise called “non-word”
Character classes
.
: matches any single character except the terminator\d
: Matches any Arabic numeral. The equivalent of[0-9]
\D
: match the\d
. The equivalent of[^ 0-9]
\w
: Matches alphanumeric characters in the basic Latin alphabet[A-Za-z0-9_]
\W
: match the\w
, which is equivalent to[^A-Za-z0-9_]
\s
: Matches whitespace characters, including the following, equivalent to[\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
:- Space character
- TAB character
- Carriage Return character
- New Line character
- Vertical TAB character
- Form Feed Character
\S
: match the\s
, which is equivalent to[^\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
\t
Match:TAB character
\r
Match:Carriage Return character
\n
Match:New Line character
\v
Match:Vertical TAB character
\f
Match:Form Feed Character
[\b]
Match:Backspace
\ 0
: Matches NULL characters\xhh
: Matches two hexadecimal numbers\uhhhh
: Matches four hexadecimal and Unicode characters
Group and the scope of
x|y
Match:x
ory
character[xyzA-Z]
Match:A
toZ
The character collection ofxyz
Three characters in which-
Is a hyphen and will be matched as a normal character if placed at the beginning or end[^xyzA-Z]
Matching except:[xyzA-Z]
Characters other than(x)
: Capture group, matchx
And rememberx
\n
:n
Is a positive integer that references the previous capture, for example:/abc(d)ef\1/
matchingabcdefg
,abcdefd
quantifiers
x*
Will:x
Match 0 times or morex+
Will:x
Match more than oncex?
Will:x
Matches 0 or 1 timesx{n}
Will:x
matchingn
timex{n,}
Will:x
matchingn
More than oncex{n,m}
Will:x
matchingn
More than once,m
Time in the followingx*? , x+? , x?? , x{n}? , x{n,}? , x{n,m}?
: originallyx*, x+, x? , x{n}, x{n,}, x{n,m}
The one that matches is greedy, plus?
After that, the match becomes “non-greedy.”
PS: “greedy” means all matches, “non-greedy” means only one match
Regular expression flag
Regular expressions have six optional arguments (flags) to allow global and case-insensitive searches, etc. These parameters can be used alone or together in any order and are included in the regular expression instance
g
: Global searchi
: Case insensitive searchm
: multi-line searchs
That would allow.
Matches a newline characteru
: Matches using patterns of Unicode codesy
: Performs the “sticky” search and matches the current position of the target string
reference
MDN- Regular expression