正则表达式备忘

正则表达式常用语法速查备忘,涵盖字符类、量词、边界、分组等常用参考。

正则表达式备忘清单

普通字符

[abc]普通字符
[^abc]空白字符
[a-zA-Z]字符集
.转义

空白字符

\tTab
\nNew line
\rCarriage return
\sAny whitespace [ \t\n\r\f\v]
\SAny non-whitespace [^ \t\n\r\f\v]

字符集

\dDigit [0-9]
\DNon-digit [^0-9]
\wWord [a-zA-Z0-9_]
\WNon-word [^a-zA-Z0-9_]
\vVertical whitespace
\VNon-vertical whitespace
\hHorizontal whitespace
\HNon-horizontal whitespace

转义 (outside [...])

\\Literal backslash
\+Literal plus
\*Literal asterisk
\?Literal question mark
\.Literal dot
\[ / \]Literal brackets
\^ / \$Literal anchors

转义 (inside [...])

\-Literal hyphen
\\Literal backslash
\]Literal closing bracket

量词

*Zero or more (greedy)
+One or more (greedy)
?Zero or one (greedy)
{n}Exactly n times
{n,}At least n times
{n,m}Between n and m times
*?Zero or more (lazy)
+?One or more (lazy)
??Zero or one (lazy)

边界

^Start of string/line
$End of string/line
\bWord boundary
\BNon-word boundary
\AStart of string
\ZEnd of string (before final newline)
\zAbsolute end of string

匹配

|Alternation (OR)
(?#...)Comment (inline)
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind
(?i)Case insensitive mode
(?m)Multiline mode
(?s)Dotall mode (dot matches newline)

分组与捕获

(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
\1, \2, ...Backreference by number
\k<name>Backreference by name

关于 正则表达式备忘

正则表达式备忘整理了对正则表达式开发中最常用的语法元素,按分类展示:普通字符、空白字符、字符集、需要转义的特殊字符(字符集内外分别说明)、量词、边界匹配、回溯引用和分组捕获。

内容为静态参考信息,无需网络即可离线查阅。

常见问题

这个备忘包含哪些内容?

涵盖普通/空白字符、字符集、转义规则、量词(*+?{n,m})、边界匹配(^$\b)、前瞻/后顾断言、分组与捕获、回溯引用等正则表达式的核心语法。适合日常开发时快速查阅。

正则表达式中的 . 和 \. 有什么区别?

在字符集外部,. 匹配除换行符外的任意单字符;\. 匹配字面意义的句号(因为 . 是特殊元字符需要转义)。在字符集 [ ] 内部,. 就是字面意义的句号,不需要转义。

会持续更新吗?

会的。我们会根据开发者反馈和 ECMAScript 正则特性的演进持续补充更多内容。如有建议欢迎通过 GitHub Issues 提交。

相关工具