1. Character metacharacters
[ ... ] |
matches any character in the list |
[ ^... ] |
matches any character not in the list |
. |
Matches any character except carriage return and line feed |
\w |
Equivalent to [ A-Za-z0-9_ ], matching any letters, numbers, underline characters, such characters are also called word characters. |
\W |
Equivalent to [ ^A-Za-z0-9_ ], does not match any letters, numbers, underscore characters. |
\d |
Equivalent to [ 0-9 ], matches any single digit character. |
\D |
Equivalent to [ ^0-9 ], matches any single character except digits. |
\s |
Match whitespace characters, which refer to spaces, tab characters, and carriage return and line feed characters. |
\S |
Matches any non-whitespace character. |
2. Repeating metacharacters
x? |
Matches 0 or 1 x. |
x+ |
Match one or more x, i.e. match at least one x. |
x* |
Match 0 or more x's. |
x{ n } |
Matches n x's. |
x{ n, } |
match n x |
x{ n,m } |
Match at least n x and at most m x, that is, match n to m x. |
3. Positioning meta characters
^ |
match must occur at the beginning of the string or the beginning of the line |
$ |
A match must occur at: end of string, before \n at end of string, or end of line. |
\b |
matches the start or end of a word |
\B |
matches the start or end of a non-word |
x( ? = y ) |
Positive positive check. Only when x is followed by y, can x be matched successfully. |
x( ? ! y ) |
Positive negative lookup. Only when x is not followed by y, can x be matched successfully. |
( ?<= x )y: |
Reverse affirmative precheck. Only when y is preceded by x, the match y is successful. Not supported in JS. (Reverse, etc., add < before forward etc.) |
( ?< ! x )y: |
Reverse negative lookup. Only when y is not preceded by x, y is matched successfully. Not supported in JS. (The reverse is not equal, add < before the forward etc.) |
4. Grouping and replacing characters
x | y |
match x or y |
( content ) |
Back reference, handle the subexpression content as a whole, the role of parentheses is equivalent to the role of parentheses in algebra, and save the captured submatches in \1, \2, ... and $1, $2, ......middle. For example: /a(bc)+/ |
(?: content) |
Group subpatterns, but do not capture subpatterns. It works similarly to (content) and is called a memoryless match. |
\1,\2,\3,... |
In the regular expression, respectively contain strings matching the first backreference, the second backreference, and the third backreference in the regular expression. |
$1,$2,$3,... |
In the replacement, include the first backreference and the second backreference in the regular expression, respectively. The third backreference matches the string. |
5. Special characters
Special characters are those characters that are not easy to express directly in writing or input directly using the keyboard
\ 0 |
Match the null character, the corresponding hexadecimal value is \x00 |
\ b |
Match the backspace character, the corresponding hexadecimal value is \x08 |
\ n |
Match a newline character, the corresponding hexadecimal value is \x0A |
\ r |
Match the carriage return character, the corresponding hexadecimal is \x0D |
\ f |
Match the form feed character, the corresponding hexadecimal is \x0C |
\ t |
Matches the tabulation (TAB) character, corresponding to both the horizontal tab character \x09 and the vertical tab character \x0B |
\xhh |
Matches characters represented by 2 hexadecimal digits |
\ uhhhh |
Matches characters represented by 4 hexadecimal digits, which are Unicode characters |
\ cchar |
Match named control characters |
6. Characters that need to be escaped
. * + ( ) $ / \ ? [ ] ^ { } - ! < >
7. Common regular expressions
[ \u4e00-\u9fa5 ] |
match any character |
[ 1- 9 ]?[ 0-9 ] | 100 |
Matches numbers from 0-100 |
\n [ \s| ] * \r |
Regular expression to match empty lines |