正则表达式参考
正则语法速查表与常用正则集合。数据在浏览器本地处理,不会上传到服务器
正则表达式参考
Metacharacters & Anchors
| Pattern | Description | Copy |
|---|---|---|
. | Any character except newline Example: a.b matches "acb" | |
\d | Digit (0-9) Example: \d{3} matches "123" | |
\D | Non-digit Example: \D matches "a" | |
\w | Word character (a-z, A-Z, 0-9, _) Example: \w+ matches "hello" | |
\W | Non-word character Example: \W matches "@" | |
\s | Whitespace (space, tab, newline) Example: \s matches " " | |
\S | Non-whitespace Example: \S matches "a" | |
\b | Word boundary Example: \bword\b matches "word" | |
\B | Non-word boundary Example: \B matches inside "word" | |
^ | Start of string Example: ^hello matches "hello" | |
$ | End of string Example: world$ matches "world" |
Quantifiers
| Pattern | Description | Copy |
|---|---|---|
* | Zero or more times Example: ab*c matches "ac", "abc", "abbc" | |
+ | One or more times Example: ab+c matches "abc", "abbc" | |
? | Zero or one time (optional) Example: colou?r matches "color", "colour" | |
{n} | Exactly n times Example: \d{3} matches "123" | |
{n,} | At least n times Example: \d{2,} matches "123" | |
{n,m} | Between n and m times Example: \d{2,4} matches "123" | |
*? | Lazy: zero or more (non-greedy) Example: <.*?> matches "<a>" | |
+? | Lazy: one or more (non-greedy) Example: <.+?> matches minimal | |
?? | Lazy: zero or one (non-greedy) Example: colou??r matches "color" |
Character Classes
| Pattern | Description | Copy |
|---|---|---|
[abc] | Any character in the set Example: [abc] matches "a", "b", or "c" | |
[^abc] | Any character NOT in the set Example: [^abc] matches "d" | |
[a-z] | Range from a to z Example: [a-z]+ matches "hello" | |
[0-9] | Range from 0 to 9 Example: [0-9]+ matches "123" |
Groups, Lookaround & Backreferences
| Pattern | Description | Copy |
|---|---|---|
(abc) | Capturing group Example: (\w+)\s\1 matches "hello hello" | |
(?:abc) | Non-capturing group Example: (?:https?): matches "http:" | |
(?=abc) | Positive lookahead Example: \d(?=px) matches "2" in "2px" | |
(?!abc) | Negative lookahead Example: \d(?!px) matches "2" in "2em" | |
(?<=abc) | Positive lookbehind Example: (?<=\$)100 matches "$100" | |
(?<!abc) | Negative lookbehind Example: (?<!\$)100 matches "100" | |
\1 | Backreference to group 1 Example: (.)\1 matches "aa" | |
| | Alternation (OR) Example: cat|dog matches "cat" or "dog" |
Flags
| Pattern | Description | Copy |
|---|---|---|
g | Global: find all matches Example: /abc/g finds all "abc" | |
i | Case-insensitive Example: /abc/i matches "ABC" | |
m | Multiline: ^ and $ match line start/end Example: /^hello/m matches after newline | |
s | Dotall: . matches newline Example: /a.b/s matches "a\nb" | |
u | Unicode: enable Unicode features Example: /\p{L}/u matches any letter | |
y | Sticky: match from lastIndex Example: /abc/y matches at exact position |