正則表達式速查表
正則表達式語法速查表,包含常見模式、字符集、量詞和其他參考
💡 關於正則表達式速查表:
我們全面的
正則表達式速查表
是正則表達式的完整參考指南。包含常見模式、字符集、量詞、錨點、分組和標誌。使用內置測試器實時測試您的正則表達式。非常適合開發人員、數據分析師以及任何從事模式匹配和文本驗證的人員。
⚡ 快速測試
測試結果將顯示在這裏
📝 常用字符
| 模式 | 説明 | 示例 |
|---|---|---|
.
|
任意字符(除換行符) | 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等)
• 標誌和修飾符參考
• 實時正則表達式測試工具
🔍 正則表達式使用場景:
• 表單驗證(郵箱、電話、密碼)
• 從文本中提取數據
• 字符串解析和操作
• 搜索和替換操作
• 日誌文件分析
• 網絡爬蟲和數據清洗
• 代碼重構和自動化