正则表达式

正则表达式速查表

完整的正则表达式语法参考,包含分类模式、量词、环视断言和实用示例。

regexcheat sheetreferencepatternsyntax

Common Patterns

Email
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
Example: test@example.com
Basic email validation
URL
/https?:\/\/[^\s]+/
Example: https://example.com/path
Match HTTP/HTTPS URLs
IPv4 Address
/\b(?:\d{1,3}\.){3}\d{1,3}\b/
Example: 192.168.1.1
IPv4 address pattern
Chinese Phone
/^1[3-9]\d{9}$/
Example: 13812345678
Mainland China mobile number
Date (YYYY-MM-DD)
/\d{4}-\d{2}-\d{2}/
Example: 2024-01-15
ISO date format

Character Classes

PatternNameExampleDescription
.Any charactera.c → abc, aXcMatches any single character except newline
\dDigit\d+ → 123Matches any digit (0-9)
\DNon-digit\D+ → abcMatches any non-digit character
\wWord character\w+ → hello_1Matches letters, digits, underscore
\WNon-word character\W+ → !@#Matches non-word characters
\sWhitespacea\sb → a bMatches space, tab, newline
\SNon-whitespace\S+ → helloMatches any non-whitespace character
[abc]Character set[aeiou] → a, e, iMatches any character in set
[^abc]Negated set[^0-9] → a, bMatches any character not in set
[a-z]Character range[a-z]+ → helloMatches any character in range

Quantifiers

PatternNameExampleDescription
*Zero or moreab*c → ac, abc, abbcMatches 0 or more of preceding
+One or moreab+c → abc, abbcMatches 1 or more of preceding
?Zero or oneab?c → ac, abcMatches 0 or 1 of preceding
{n}Exact counta{3} → aaaMatches exactly n occurrences
{n,}n or morea{2,} → aa, aaaMatches n or more occurrences
{n,m}Between n and ma{2,4} → aa, aaa, aaaaMatches between n and m occurrences
*?Lazy zero or morea.*?b → abNon-greedy matching
+?Lazy one or morea.+?b → abNon-greedy matching

Anchors

PatternNameExampleDescription
^Start of string^hello → hello worldMatches at start of string/line
$End of stringworld$ → hello worldMatches at end of string/line
\bWord boundary\bword\b → the wordMatches word boundary
\BNon-word boundary\Bword → swordMatches non-word boundary
(?=...)Positive lookaheada(?=b) → abMatches if followed by pattern
(?!...)Negative lookaheada(?!b) → acMatches if not followed by pattern
(?<=...)Positive lookbehind(?<=a)b → abMatches if preceded by pattern
(?<!...)Negative lookbehind(?<!a)b → cbMatches if not preceded by pattern

Groups

PatternNameExampleDescription
(...)Capturing group(ab)+ → ababCaptures matched text
(?:...)Non-capturing group(?:ab)+ → ababGroups without capturing
(?<name>...)Named group(?<year>\d{4})Captures with name
\1Backreference(\w)\1 → aa, bbMatches captured group again
a|bAlternationcat|dog → cat, dogMatches either expression

Assertions

PatternNameExampleDescription
(?=...)Positive lookahead\d(?=px) → 10pxAssert pattern follows
(?!...)Negative lookahead\d(?!px) → 10emAssert pattern does not follow
(?<=...)Positive lookbehind(?<=\$)\d+ → $100Assert pattern precedes
(?<!...)Negative lookbehind(?<!\$)\d+ → €100Assert pattern does not precede

Flags

PatternNameExampleDescription
gGlobal/a/g → a, a, aFind all matches, not just first
iCase insensitive/abc/i → ABC, abcIgnore case in matching
mMultiline/^a/m → ... a^ and $ match line boundaries
sDotAll / Singleline/a.*b/s → a b. matches newlines too
uUnicode/\u{1F600}/uEnable Unicode support

What is 正则表达式速查表?

正则表达式速查表与参考是一款全面的快速参考指南,涵盖JavaScript(PCRE兼容)模式的正则表达式语法:锚点、量词、字符类、分组、反向引用和环视断言。正则表达式是文本处理的强大工具,但其密集的语法出了名地难以记忆— 即使经验丰富的开发者也经常需要查找贪婪与惰性量词之间的区别、正向前瞻的确切语法,或者哪些字符类在括号内需要转义。本速查表将语法组织为逻辑分组的部分:字符(字面量、字符类、简写)、锚点(边界)、量词(贪婪、惰性、占有)、分组(反向引用)、前瞻/后顾断言、标志(修饰符)、替换模式和常用配方。每个条目包括模式语法、通俗易懂的解释和可测试的工作示例。常用配方部分为40多个最常见的正则任务提供生产就绪的模式:邮箱验证、IPv4/IPv6地址、电话号码、URL、日期、信用卡号、密码复杂度、HTML标签提取等。模式条目包括复制按钮和到正则测试器的直接链接以进行即时实验。

When to Use 正则表达式速查表

用于构建表单验证的正则模式、查找环视语法、为常见任务查找预构建的正则配方、记忆量词行为,或向队友教授正则概念。

How to Use 正则表达式速查表

浏览分类的语法部分或搜索特定概念。每个条目显示语法、解释和工作示例。单击测试直接跳转到预加载模式的正则测试器。