Recommended first-read regular expressions must know this book
Placed behind an indefinite quantifier (after a quantifier such as * +)? is to represent a non-greedy match
Greedy match
str = "AB1111BA111BA";
reg =/ab[\s\s]+ba/;
Console.log (Str.match (reg));
Non-greedy match
str = "AB1111BA111BA";
reg =/ab[\s\s]+?ba/;
Console.log (Str.match (reg));
Placed behind a character? Indicates a 0~1 occurrence
/https?/.exec (' http://xxxx ') get HTTP
/https?/.exec (' https://xxxx ') get HTTPS
[] is to define multiple characters as a collection
When [] and? When used together should []?
B for non-word boundary/\b-\b/.exec (' xcxc wd-cdsd--')
b for Word boundaries (Word: alphanumeric underline)/\b_\b/.exec (' Xcxc _ wd-cdsd--')
PS/\b-\b/.exec (' xcxc _ wd-cdsd--') This is not getting results
^ Only in cases where [^ ...] is not
Other times indicate the beginning
For example, I want to match a room with a non-B
/\b^b\d+\b/.exec (' b123 d234 s234 b4553 ')
["B123"]
Results do not add [] but only match the room with the beginning of B
/\b[^b]\d+\b/.exec (' b123 d234 s234 b4553 ')
["d234"]
Generally speaking ^ match beginning $ match end
(? m) in multiline mode ^ $ will match the beginning and end of each line
19|20\D{2} This does not match 19xx 20xx, because | It is a whole
It only matches numbers like 20xx.
/(19|20) \d{2}/
() sub-expression
() In addition to grouping there is a very important function which can extract the contents of the sub-expression
The contents of PS () are called sub-expressions
STR3 = ' The S is ac/0923/d456 ';
Str3.match (/[a-z]{2}\/\d{4}\/[a-z]\d{3}/)
Get ["ac/0923/d456"]
If the present requirement is to convert all ac/0923/d456 to ac-0923-d456 form
In this case, we need to use sub-expressions to remove the AC 0923 d456 separately.
Str3.match (/([a-z]{2}) \ (\d{4})/([a-z]\d{3})/)
["ac/0923/d456", "AC", "0923", "d456"]
You can then use variable expressions such as $ A
Str3.replace (/([a-z]{2}) \ (\d{4})/([a-z]\d{3})/, ' $1-$2-$3 ')
"The S is ac-0923-d456"
PS $ A variable reference must have sub-expression
() grouping function
/[\d]{3}-[\d]{3}-[\d]{3}-[\d]{3}/.test (' 333-333-222-233 ')
The same part was written several times actually using () can be abbreviated
/([\d]{3}-) {3}[\d]{3}/.test (' 333-333-222-233 ')
Regular expressions must be known