功能说明
Regex Explainer 把一个正则表达式变成通俗易懂、逐 token 的拆解说明。每个 token——锚点、字符类、量词、分组、转义、交替或字面量——都有自己的一行,展示确切的片段及其匹配内容,外加 token 计数,以及对每个已设置 flag 的解释。无效的模式会在红色错误框中显示 JavaScript 引擎自己的报错,而不是让页面崩溃。无论是学习正则,还是破译在代码库里找到的一段表达式,它都很有用。100% 客户端;不向任何地方发送数据。
使用方法
- 输入模式(不含分隔符)。拆解随你的输入实时更新。
- 可选设置 Flags ——该字段接受
g i m s u d y;其他字符会被剥除。 - 或点击示例芯片——Email、Date capture、URL、Lookahead——载入现成的模式和配套 flags。
- 在右侧阅读报告(桌面端固定不动):每个 token 一行,顶部有 token 计数徽章,每个激活的 flag 都附有含义。
- Copy breakdown 把完整解释复制为纯文本;地址栏始终携带你的模式(
?p=和?f=),复制 URL 即可分享这个精确视图。
无效正则(例如未闭合的 [)会显示引擎的错误信息——[ 对应 Invalid regular expression: missing terminating ] for character class——并带 Invalid 徽章,绝不抛出异常。
示例
以下所有解释都是工具对每个输入的真实输出。
类电话模式 —— ^\d{3}-\d{4}$ 加 flag g(内置 Sample,7 个 token):
| Token | 解释 |
|---|---|
^ |
start of the string (or line with /m) |
\d |
a digit [0-9] |
{3} |
quantifier - repeat 3 time(s) |
- |
the literal “-” |
\d |
a digit [0-9] |
{4} |
quantifier - repeat 4 time(s) |
$ |
end of the string (or line with /m) |
Flag g → global - find all matches。
类 Email 模式 —— [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,} 加 flags gi(Email 芯片,8 个 token),节选:
[a-zA-Z0-9._%+-]→match any of: a-zA-Z0-9._%+-+→quantifier - 1 or more times (greedy)@→the literal "@"\.→an escaped literal "."{2,}→quantifier - repeat 2, time(s)- Flag
i→case-insensitive
日期捕获 —— (\d{4})-(\d{2})-(\d{2})(Date capture 芯片,5 个 token):三行 capturing group(分别对应 (\d{4})、(\d{2})、(\d{2})),中间隔着两行 the literal "-"。
先行断言 —— \d+(?=px)(Lookahead 芯片,3 个 token):a digit [0-9]、quantifier - 1 or more times (greedy),然后是 (?=px) → lookahead assertion (positive)。
补充说明
- Flags 参考(每个 flag 的含义都渲染在其字母旁):
gglobal - find all matches ·icase-insensitive ·mmultiline (^ and $ match line boundaries) ·sdotAll - “.” matches newlines ·uunicode ·ysticky - match at lastIndex ·dindices - expose match boundaries。 - 支持 JS
RegExp语法;其他引擎的扩展(POSIX 字符类、不带u的\p{...})解析结果可能不同。 - 私密: 解析完全在本地、在你的浏览器中完成。
- 相关工具: Regex Tester(实时运行匹配)、Find & Replace、JSON to TypeScript。