广告

RegEx Tester

测试和调试常规 表达式

未找到匹配项

常见模式

什么是常规 表达方式?

正则表达式(regex 或 regexp)是用于匹配字符组合的模式 字符串。它们是一个非常强大的工具,用于搜索、验证、提取和 替换文本。正则表达式最初开发于 20 世纪 50 年代,现在几乎所有领域都支持正则表达式 编程语言和文本编辑器。

该工具使用 JavaScript 的正则表达式引擎,该引擎实现了 ECMAScript 标准。测试你的 在您的代码中使用它们之前,先将这些模式放在这里 - 所有处理都在您的浏览器中进行,没有数据 发送到服务器。

正则表达式语法快速参考

字符 类

  • \d - Any digit (0-9)
  • \w - Word character (a-z, A-Z, 0-9, _)
  • \s - Whitespace (space, tab, newline)
  • . - Any character except newline
  • [abc] - Any of a, b, or c
  • [^abc] - Not a, b, or c

量词

  • * - 0 or more
  • + - 1 or more
  • ? - 0 or 1 (optional)
  • {n} - Exactly n times
  • {n,} - n or more times
  • {n,m} - Between n and m times

锚点 & 边界

  • ^ - Start of string/line
  • $ - End of string/line
  • \b - Word boundary
  • \B - Non-word boundary

组& 交替

  • (abc) - Capture group
  • (?:abc) - Non-capture group
  • a|b - Either a or b
  • \1 - Backreference to group 1

了解标志

g(全局) - 查找所有匹配项,而不仅仅是第一个匹配项 one
i(忽略大小写) - 不区分大小写的匹配 (A 匹配 a)
m(多行) - ^ 和 $ 匹配每个的开始/结束 行,而不仅仅是字符串
s (dotAll) - 使 .也匹配换行符

常见问题

为什么我需要转义 反斜杠?

In JavaScript strings, backslash is an escape character. To include a literal backslash in a regex pattern from a string, you need \\. For example, \\d in a string becomes \d in the regex.

有什么区别 * 和 + 之间?

* matches zero or more occurrences (can match nothing). + requires at least one occurrence. For example, a* matches "", "a", "aaa", while a+ only matches "a", "aaa".

如何匹配文字 句号或括号?

Special regex characters need to be escaped with a backslash. To match a literal period, use \.. For brackets: \[ and \]. Characters that need escaping: . * + ? ^ $ { } [ ] \ | ( )

为什么是我的模式 匹配太多?

Regex quantifiers are "greedy" by default—they match as much as possible. Add ? after a quantifier for "lazy" matching (e.g., .*? instead of .*).

常见用例

  • 表单验证: 验证电子邮件、电话 数字、URL 和自定义格式
  • 搜索和替换: 寻找并转变 编辑器和代码中的文本模式
  • 日志解析: 提取时间戳、IP、 日志文件中的错误代码和错误代码
  • 数据提取: 提取特定数据 来自 HTML、JSON 或非结构化文本
  • 输入清理: 删除或转义 危险字符