正则表达式速查表
正则表达式语法速查表,包含常见模式、字符集、量词和其他参考
💡 关于正则表达式速查表:
我们全面的
正则表达式速查表
是正则表达式的完整参考指南。包含常见模式、字符集、量词、锚点、分组和标志。使用内置测试器实时测试您的正则表达式。非常适合开发人员、数据分析师以及任何从事模式匹配和文本验证的人员。
⚡ 快速测试
测试结果将显示在这里
📝 常用字符
| 模式 | 说明 | 示例 |
|---|---|---|
.
|
任意字符(除换行符) | a.c 匹配 abc, aac |
\d
|
数字 [0-9] | \d{3} 匹配 123 |
\D
|
非数字 | \D+ 匹配 abc |
\w
|
字母数字下划线 [a-zA-Z0-9_] | \w+ 匹配 word_123 |
\W
|
非单词字符 | \W+ 匹配 !@# |
\s
|
空白字符(空格、制表符等) | \s+ 匹配空白 |
\S
|
非空白字符 | \S+ 匹配 abc |
🔐 转义字符
| 模式 | 说明 | 示例 |
|---|---|---|
\.
|
匹配点号本身 | \.com 匹配 .com |
\\
|
匹配反斜杠 | \\ 匹配 \ |
\*
|
匹配星号 | \* 匹配 * |
\+
|
匹配加号 | \+ 匹配 + |
\?
|
匹配问号 | \? 匹配 ? |
\(
|
匹配左括号 | \( 匹配 ( |
\)
|
匹配右括号 | \) 匹配 ) |
\[
|
匹配左方括号 | \[ 匹配 [ |
\]
|
匹配右方括号 | \] 匹配 ] |
\{
|
匹配左花括号 | \{ 匹配 { |
\}
|
匹配右花括号 | \} 匹配 } |
\|
|
匹配竖线 | \| 匹配 | |
\-
|
匹配连字符 | \- 匹配 - |
\^
|
匹配脱字符 | \^ 匹配 ^ |
\$
|
匹配美元符号 | \$ 匹配 $ |
🔢 Quantifiers
| Pattern | Instructions | Example |
|---|---|---|
*
|
0 or more times | ab* matches a, ab, abb |
+
|
1 or more times | ab+ matches ab, abb |
?
|
0 or 1 time | ab? matches a, ab |
{n}
|
Exactly n times | a{3} matches aaa |
{n,}
|
At least n times | a{2,} matches aa, aaa |
{n,m}
|
Between n and m times | a{2,4} matches aa, aaa, aaaa |
*?
|
Non-greedy 0 or more times | <.+?> matches shortest HTML tag |
+?
|
Non-greedy 1 or more times | <.+?> matches shortest HTML tag |
📍 Boundary Anchors
| Pattern | Instructions | Example |
|---|---|---|
^
|
string start | ^abc matches abc... |
$
|
string end | abc$ matches ...abc |
\b
|
word boundary | \bword\b matches word |
\B
|
Non-word boundary | \Bword\B matches sword |
\A
|
String start (multiline) | \Aabc matches abc |
\Z
|
String end (multiline) | abc\Z matches abc |
🎨 Character Sets
| Pattern | Instructions | Example |
|---|---|---|
[abc]
|
Matches a, b, or c | [abc] matches a, b, c |
[^abc]
|
Does not match a, b, or c | [^abc] matches d, e, f |
[a-z]
|
Lowercase letters | [a-z] matches a to z |
[A-Z]
|
Uppercase letters | [A-Z] matches A to Z |
[0-9]
|
Digits | [0-9] matches 0 to 9 |
[a-zA-Z]
|
All letters | [a-zA-Z] matches a-z, A-Z |
[\u4e00-\u9fa5]
|
Chinese characters | [\u4e00-\u9fa5]+ matches Chinese characters |
📦 Grouping and Capturing
| Pattern | Instructions | Example |
|---|---|---|
(abc)
|
capturing group | (\d{3}) matches and captures digits |
(?:abc)
|
Non-capturing group | (?:\d{3}) matches but does not capture |
(?
|
Named capturing group |
(?
|
\1
|
Reference group 1 | (\w)\1 matches aa, bb |
\2
|
Reference group 2 | (\w)(\w)\2 matches aba |
(?=abc)
|
positive lookahead | \w+(?=\.com) matches domain name |
(?!abc)
|
negative lookahead | \w+(?!\.com) does not match .com |
(?<=abc)
|
positive lookbehind | (?<=\$)\d+ matches amount |
(?
|
negative lookbehind | (? |
🎯 Common Regex Patterns
| purpose | regular expression | Instructions |
|---|---|---|
^\w+@\w+\.\w+$
|
Basic email format | |
| phone number |
^1[3-9]\d{9}$
|
China mainland phone number |
| ID number |
^\d{17}[\dXx]$
|
18-digit ID number |
| URL |
https?:\/\/[\w\-\.]+(:\d+)?(\/.*)?
|
HTTP/HTTPS link |
| IP address |
^(\d{1,3}\.){3}\d{1,3}$
|
IPv4 address |
| date |
^\d{4}-\d{2}-\d{2}$
|
YYYY-MM-DD format |
| time |
^\d{2}:\d{2}:\d{2}$
|
HH:MM:SS format |
| Chinese characters |
[\u4e00-\u9fa5]
|
Single Chinese character |
| HTML tag |
<[^>]+>
|
Matches HTML tag |
| hex color |
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
|
#FFF or #FFFFFF |
| username |
^[a-zA-Z0-9_]{3,16}$
|
3-16 characters |
| strong password |
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
|
Uppercase + lowercase + number + 8 chars |
🚩 Flags
| flag | Instructions | Example |
|---|---|---|
g
|
Global match, find all matches | /a/g matches all a |
i
|
Ignore case | /abc/i matches ABC, abc |
m
|
Multiline pattern, ^$ matches line start/end | /^abc/m matches line start |
s
|
Single-line pattern, . matches newline | /a.b/s matches a\nb |
u
|
Unicode pattern | /\u{1F600}/u matches emoji |
y
|
Sticky match, starts from lastIndex | /a/y strictly starts from index |
💡 使用说明
快速复制:
点击表格下方的按钮可快速复制常用正则表达式
实时测试: 在顶部测试区域输入文本和正则表达式,点击"测试匹配"查看结果
JavaScript示例:
常用方法:
• test() - 测试是否匹配
• exec() - 执行匹配并返回结果
• match() - 查找匹配项
• replace() - 替换匹配项
• split() - 按模式分割字符串
实时测试: 在顶部测试区域输入文本和正则表达式,点击"测试匹配"查看结果
JavaScript示例:
const regex = /\d+/g;
const matches = "abc123".match(regex); // ["123"]
常用方法:
• test() - 测试是否匹配
• exec() - 执行匹配并返回结果
• match() - 查找匹配项
• replace() - 替换匹配项
• split() - 按模式分割字符串
📝 正则表达式速查表功能:
• 完整的正则表达式语法参考
• 常用字符集和模式
• 量词和重复操作符
• 锚点和边界
• 分组和捕获
• 先行和后行断言
• 常用正则表达式模式(邮箱、电话、URL等)
• 标志和修饰符参考
• 实时正则表达式测试工具
🔍 正则表达式使用场景:
• 表单验证(邮箱、电话、密码)
• 从文本中提取数据
• 字符串解析和操作
• 搜索和替换操作
• 日志文件分析
• 网络爬虫和数据清洗
• 代码重构和自动化
• 完整的正则表达式语法参考
• 常用字符集和模式
• 量词和重复操作符
• 锚点和边界
• 分组和捕获
• 先行和后行断言
• 常用正则表达式模式(邮箱、电话、URL等)
• 标志和修饰符参考
• 实时正则表达式测试工具
🔍 正则表达式使用场景:
• 表单验证(邮箱、电话、密码)
• 从文本中提取数据
• 字符串解析和操作
• 搜索和替换操作
• 日志文件分析
• 网络爬虫和数据清洗
• 代码重构和自动化