🔍 Regex Tester

📐 工作原理与说明
用 new RegExp(表达式, 修饰符) 编译后匹配:带 g 时循环调用 exec 收集全部结果,遇到零宽匹配(match.index 等于 lastIndex)时手动把 lastIndex 加一以防死循环,匹配数超过 1000 条提前终止;明细列表最多展示前 100 条、高亮最多覆盖前 500 条,引擎收集与复制上限为 1000 条;不带 g 时只取首个匹配。统计:匹配数即收集条数,捕获组数 = 首个匹配长度 − 1;端到端耗时 = 主线程 performance.now 起止差(含 Worker 创建、消息传递与主线程调度,不是纯正则引擎耗时),Worker 内的匹配计算耗时另列;单次测量受设备与线程调度影响,不能直接作为表达式性能基准;表达式为空时不执行匹配并清零统计
正则表达式测试工具:匹配、提取与替换。
  • 元字符:. * + ? ^ $ [] () | \d \w \s。
  • 标志:g 全局、i 忽略大小写、m 多行、s 使 . 匹配换行、u 启用 Unicode。
  • 用途:校验格式、批量提取、文本替换。
  • 风险:嵌套量词(如 (a+)+)会引发灾难性回溯,导致 CPU 耗尽。

Test and debug regular expressions with live matching; capture groups are listed per match.

/ /
匹配数: 0 分组数: 0 端到端耗时: 0ms 匹配计算: 0ms(Worker 内)

使用浏览器 JavaScript 正则引擎;结果与前端 RegExp 行为一致。

By default only the pattern and flags are shared. If enabled, the test text is encoded into the URL fragment; encoding is not encryption, so logs or credentials may be exposed and the link may remain in browser history or the clipboard.
History is stored locally. By default only the pattern, flags and result summary are saved; test text is not saved unless enabled. The CSV export first writes the current match list (# / match / groups / index), falling back to the history summary table when there are no matches.
输入正则后将自动匹配...
高亮结果将显示在这里...

替换语法: $& 匹配的文本   $1 第1个分组   $` 匹配前文本   $' 匹配后文本

替换数: 0 原长度: 0 新长度: 0
替换结果将显示在这里...

📖 正则语法速查

语法说明示例
.匹配任意字符(除换行)a.c 匹配 abc
\d数字 [0-9]\d+ 匹配 123
\w单词字符 [a-zA-Z0-9_]\w+ 匹配 hello_123
\s空白字符\s+ 匹配空格
^行首^Hello 匹配行首Hello
$行尾end$ 匹配行尾end
*0次或多次a* 匹配 空、a、aaa
+1次或多次a+ 匹配 a、aaa
?0次或1次colou?r 匹配 color/colour
{n}恰好n次\d{3} 匹配 3位数字
{n,}至少n次\d{2,} 匹配 2位以上
{n,m}n到m次\d{2,4} 匹配 2-4位
[abc]字符集[aeiou] 匹配元音
[^abc]排除字符集[^0-9] 匹配非数字
(abc)捕获分组(\d+)-(\d+) 捕获两组
(?:abc)非捕获分组(?:ab)+ 不捕获
a|b或cat|dog 匹配cat或dog
(?=abc)正向先行断言\d(?=px) 后跟px的数字
(?!abc)负向先行断言\d(?!px) 后不跟px的数字

📚 深度解析:正则表达式测试器

💡 常见使用场景

  • 实时测试正则匹配与捕获组。
  • 上线前先把正则放进真实样本里试:确认既不会漏掉合法输入,也不会误匹配非法输入,再写进代码。
测试
`\d{3}-\d{4}` 匹配 `123-4567`,高亮并列出分组。

❓ 常见问题(FAQ)

如何全局匹配?
加 `g` 标志遍历所有匹配。
为什么匹配卡住了?
很可能是灾难性回溯:嵌套量词(如 (a+)+)在遇到长而不匹配的字符串时,尝试次数会指数级增长,导致 CPU 打满。解决办法是避免嵌套量词、改用更具体的字符类,或用原子组/独占量词(视引擎支持)。用户可控的正则应设超时保护。

Formula & Standard References

Item Formula / Note Source / Standard
Character classes \d \w \s with negations; [] for custom sets ECMA-262 / POSIX ERE
Quantifiers * + ? and {n,m}; greedy by default ECMA-262
Backtracking risk JS has no atomic groups; avoid nested quantifiers OWASP ReDoS guidance
⚠️ 使用说明与注意事项