Summary of basic knowledge of regular expression in JavaScript

Time:2021-4-20

Metacharacter
^ $ . * + ? = ! : | \ / ( ) [ ] { }
When using these symbols, you need to use “\” to transfer.
If you can’t remember which punctuation marks need to be transferred, you can use backslashes when using punctuation marks“ \”
Simple matching
1. Direct quantity / JavaScript / matches strings with “JavaScript”, such as “JavaScript is an object-oriented scripting language”
2. [] / [ABC] / matches the character “a” or “B” or “C”
3. [^] / [^ ABC] / matches any character except a, B and C
4. . /. / any character
5、 \ w /\ w / Any single ASCII character, equivalent to [ a – zA – Z0 – nine ]
6. White space character
7. The value of [0-9]
repeat
1、 { n , m } /[ a ]{ three , five }/ Repeat at least N times, maximum m times, such as “AAA” or “AAAA” or “AAAAA”
2. {n,} / [a] {3,} / repeat at least N times, such as “AAA” or “AAAAA” or “aaaaaa”
3. The {n} / [a] / {3} matches exactly n times, for example, it can only match “AAA”
4. ? / [a]? / 0 or 1 times
5. + / [a] + / once or more
6. * / [a] * / 0 or more times
Select, group
1、 | / a | b / Select, match “a” or “B”
2. () / (ABC) + / group, match “ABC” or “abcabc”
position
1. ^ / ^ A / start with “a”, such as “ab” or “ABC”
2. $/ B $/ ends with “B”, such as “ab” or “CB”
sign
1、i / a / I case insensitive, such as “a” or “a”
2. G / B / g global match, find all matches
method
1、 var ret = “javascript” . search (/ script / i ) Returns the position of the beginning character of the first matching substring. No matching is returned – one
2. Var RET = “JavaScript”. Replace (/ Java / GI, “”) is used to replace the matching substring with the second parameter of the function;
3. Var RET = “JavaScript”. Match (/ (Java) (script) / GI) returns an array
ret [ 0 ] Complete matching “JavaScript”
RET [1] the substring “Java” matched by the first bracket
ret [ two ] The second bracket matches the substring “script”
……
4、 var ret = “javascript” . split (/ a /) Use the substring matched by the parameter as the separator to decompose the string and return the array