reference:
Regular Expressions Quick Reference
揭开正则表达式的神秘面纱
正则表达式30分钟教程
前端进阶必须知道的正则表达式知识
example:
最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
由数字、26个英文字母、下划线或汉字的正则表达式
常用的正则表达式全面总结
online tool:
RegExr
Debuggex
regex101
元字符 (需转义\)
( [ { \ ^ $ | ) ? * + . ] }
Character classes
| Pattern |
Description |
. |
Any character, except newline |
\w |
Word |
\d |
Digit |
\s |
Whitespace |
\W |
Not word |
\D |
Not digit |
\S |
Not whitespace |
[abc] |
Any of a, b, or c |
[a-e] |
Characters between a and e |
[1-9] |
Digit between 1 and 9 |
Anchors
| Pattern |
Description |
^abc |
Start with abc |
abc$ |
End with abc |
Escaped characters
| Pattern |
Description |
\. \* \\ |
Escape special character used by regex |
\t |
Tab |
\n |
Newline |
\r |
Carriage return |
Groups
| Pattern |
Description |
(abc) |
Capture group |
Quantifiers
| Pattern |
Description |
a* |
Match 0 or more |
a+ |
Match 1 or more |
a? |
Match 0 or 1 |
a{5} |
Match exactly 5 |
a{,3} |
Match up to 3 |
a{3,} |
Match 3 or more |
a{1,3} |
Match between 1 and 3 |