Regular learning
class
Character class []
An object that conforms to certain characteristics
/[ABC] / means to match one of ABC
- The reverse class [^] is enclosed in brackets^
Example: [^ A] is to match characters that are not a
Range class [A-Z]
It can match any character in A-Z
Predefined Class
- Predefined characters:
\D is a number
- Boundary character
\B is the boundary
^It means to match the beginning outside
$means to match the ending
classifier
{n} The length of the matching string is n
The minimum length of {m, n} matching string is m and the maximum length is n
The minimum length of {m,} matching string is m and the maximum is infinite
The minimum length of {0, n} matching string is empty and the maximum length is n
+Occurrence times > = 1
*Current frequency > = 0
? Occurrence times 0 or 1
. can match any character except the newline character in the white space character
Greedy mode
- 12345678. Replace (/ \ D {3,6} /,’x ‘) output X78
Non greedy mode
Just add a question mark after the quantifier
- 12345678.replace(/\d{3,6}? /’x’) outputs xx78 to try the least possible match
grouping
Use () to make the quantifier most used in groups rather than the words next to it
‘a1b2c3d4’.replace(/([a-z]\d){3}/g),‘x’)
Output xd4
or
a|b
Back reference
$reference (capture)
Replace December 5, 2019 with 12 / 05 / 2019
‘2019-12-05’.replace(‘/(/d{4}-(\d{2})-(/d{2}))/g’,’$2/$3/$1′)
$is captured in groups
When you don’t want to capture a group, you just need to add the? :
(?: Gray). (OK) here $1 is OK
Looking forward and looking back
To learn
The original meaning of the character
To learn
Metacharacter (character representing special meaning)
To learn
XMind – Trial Version