正则表达式
正则表达式速查表
完整的正则表达式语法参考,包含分类模式、量词、环视断言和实用示例。
regexcheat sheetreferencepatternsyntax
Common Patterns
URL
/https?:\/\/[^\s]+/Example:
https://example.com/pathMatch HTTP/HTTPS URLs
IPv4 Address
/\b(?:\d{1,3}\.){3}\d{1,3}\b/Example:
192.168.1.1IPv4 address pattern
Chinese Phone
/^1[3-9]\d{9}$/Example:
13812345678Mainland China mobile number
Date (YYYY-MM-DD)
/\d{4}-\d{2}-\d{2}/Example:
2024-01-15ISO date format
Character Classes
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
. | Any character | a.c → abc, aXc | Matches any single character except newline | |
\d | Digit | \d+ → 123 | Matches any digit (0-9) | |
\D | Non-digit | \D+ → abc | Matches any non-digit character | |
\w | Word character | \w+ → hello_1 | Matches letters, digits, underscore | |
\W | Non-word character | \W+ → !@# | Matches non-word characters | |
\s | Whitespace | a\sb → a b | Matches space, tab, newline | |
\S | Non-whitespace | \S+ → hello | Matches any non-whitespace character | |
[abc] | Character set | [aeiou] → a, e, i | Matches any character in set | |
[^abc] | Negated set | [^0-9] → a, b | Matches any character not in set | |
[a-z] | Character range | [a-z]+ → hello | Matches any character in range |
Quantifiers
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
* | Zero or more | ab*c → ac, abc, abbc | Matches 0 or more of preceding | |
+ | One or more | ab+c → abc, abbc | Matches 1 or more of preceding | |
? | Zero or one | ab?c → ac, abc | Matches 0 or 1 of preceding | |
{n} | Exact count | a{3} → aaa | Matches exactly n occurrences | |
{n,} | n or more | a{2,} → aa, aaa | Matches n or more occurrences | |
{n,m} | Between n and m | a{2,4} → aa, aaa, aaaa | Matches between n and m occurrences | |
*? | Lazy zero or more | a.*?b → ab | Non-greedy matching | |
+? | Lazy one or more | a.+?b → ab | Non-greedy matching |
Anchors
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
^ | Start of string | ^hello → hello world | Matches at start of string/line | |
$ | End of string | world$ → hello world | Matches at end of string/line | |
\b | Word boundary | \bword\b → the word | Matches word boundary | |
\B | Non-word boundary | \Bword → sword | Matches non-word boundary | |
(?=...) | Positive lookahead | a(?=b) → ab | Matches if followed by pattern | |
(?!...) | Negative lookahead | a(?!b) → ac | Matches if not followed by pattern | |
(?<=...) | Positive lookbehind | (?<=a)b → ab | Matches if preceded by pattern | |
(?<!...) | Negative lookbehind | (?<!a)b → cb | Matches if not preceded by pattern |
Groups
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
(...) | Capturing group | (ab)+ → abab | Captures matched text | |
(?:...) | Non-capturing group | (?:ab)+ → abab | Groups without capturing | |
(?<name>...) | Named group | (?<year>\d{4}) | Captures with name | |
\1 | Backreference | (\w)\1 → aa, bb | Matches captured group again | |
a|b | Alternation | cat|dog → cat, dog | Matches either expression |
Assertions
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
(?=...) | Positive lookahead | \d(?=px) → 10px | Assert pattern follows | |
(?!...) | Negative lookahead | \d(?!px) → 10em | Assert pattern does not follow | |
(?<=...) | Positive lookbehind | (?<=\$)\d+ → $100 | Assert pattern precedes | |
(?<!...) | Negative lookbehind | (?<!\$)\d+ → €100 | Assert pattern does not precede |
Flags
| Pattern | Name | Example | Description | |
|---|---|---|---|---|
g | Global | /a/g → a, a, a | Find all matches, not just first | |
i | Case insensitive | /abc/i → ABC, abc | Ignore case in matching | |
m | Multiline | /^a/m → ... a | ^ and $ match line boundaries | |
s | DotAll / Singleline | /a.*b/s → a b | . matches newlines too | |
u | Unicode | /\u{1F600}/u | Enable Unicode support |
What is 正则表达式速查表?
正则表达式速查表与参考是一款全面的快速参考指南,涵盖JavaScript(PCRE兼容)模式的正则表达式语法:锚点、量词、字符类、分组、反向引用和环视断言。正则表达式是文本处理的强大工具,但其密集的语法出了名地难以记忆— 即使经验丰富的开发者也经常需要查找贪婪与惰性量词之间的区别、正向前瞻的确切语法,或者哪些字符类在括号内需要转义。本速查表将语法组织为逻辑分组的部分:字符(字面量、字符类、简写)、锚点(边界)、量词(贪婪、惰性、占有)、分组(反向引用)、前瞻/后顾断言、标志(修饰符)、替换模式和常用配方。每个条目包括模式语法、通俗易懂的解释和可测试的工作示例。常用配方部分为40多个最常见的正则任务提供生产就绪的模式:邮箱验证、IPv4/IPv6地址、电话号码、URL、日期、信用卡号、密码复杂度、HTML标签提取等。模式条目包括复制按钮和到正则测试器的直接链接以进行即时实验。
When to Use 正则表达式速查表
用于构建表单验证的正则模式、查找环视语法、为常见任务查找预构建的正则配方、记忆量词行为,或向队友教授正则概念。
How to Use 正则表达式速查表
浏览分类的语法部分或搜索特定概念。每个条目显示语法、解释和工作示例。单击测试直接跳转到预加载模式的正则测试器。