Character classes字符集
. any character except newline (除了换行的任意字符)
\w \d \s word, digit, whitespace (单词,数字,空格)
\W \D \S not word, digit, whitespace(非单词,数字,空格)
[abc] any of a, b, or c (a,b,c中的任意一个)
[^abc] not a, b, or c (不是a,b或者c)
[a-g] character between a & g (a-g之间的任意字符(abcdefg中的任意一个))
Anchors位置匹配符
^abc$ start / end of the string(^匹配起始位置,$匹配结束位置)
\b \B word, not-word boundary(\b单词的边界,\B非单词的边界)
Escaped characters换码符(转义字符)
\. \* \\ escaped special characters(转换特殊字符)
\t \n \r tab, linefeed, carriage return(制表符,换行,回车)
Groups & Lookaround(组与环视)
(abc) capture group(分组)
\1 backreference to group #1 (回溯引用组1,即\1匹配内容要和组1内容相同,例如
)(?:abc) non-capturing group(非捕获组,使逻辑更加清晰)
(?=abc) positive lookahead 顺序肯定环视
(?!abc) negative lookahead 顺序否定环视
(?<=abc) positive lookbehind 逆序肯定环视
(? negative lookbehind 逆序否定环视
Quantifiers & Alternation量化与交替
a* a+ a? 0 or more, 1 or more, 0 or 1(*:0个或者更多;+:1个或者更多;?:0个或者1个)
a{5} a{2,} exactly five, two or more(5个,2个或者更多)
a{1,3} between one & three(1个,2个或者3个)
a+? a{2,}? match as few as possible(尽可能少的匹配)
ab|cd match ab or cd(匹配ab或者cd)
练习网站:RegExs
进阶难点教程:回溯引用、前后查找、嵌入条件