This pattern has 2 capturing groups with 2 flags (gi) and is broken down into 14 parts below.
Flags
gglobal - find all matches in the input, not just the first oneiignore case - letters match regardless of upper or lower case^Start-of-string anchor (or start of a line with the m flag)(?<user>Start of a named capturing group "user" - saves its match under that name[a-z0-9._%+-]A single character from the set: the range "a" to "z", the range "0" to "9", the character ".", the character "_", the character "%", the character "+", the character "-"+Repeated one or more times)End of the group@A literal "@" character(?<domain>Start of a named capturing group "domain" - saves its match under that name[a-z0-9.-]A single character from the set: the range "a" to "z", the range "0" to "9", the character ".", the character "-"+Repeated one or more times\.A literal "." character (the backslash removes its special meaning)[a-z]A single character from the set: the range "a" to "z"{2,}Repeated 2 or more times)End of the group$End-of-string anchor (or end of a line with the m flag)
正则表达式解释器采用正则表达式并将其翻译成简单的英语,一次一个结构。 而不是像 ^(?<user>[a-z0-9._%+-]+)@ 那样斜眼看... 你得到一个标记的故障:这部分是一个锚,这是一个命名捕获组,这个字符类的意思是“一个小写字母、一个数字、一个点或一个连字符”,这个量词的意思是“一次或多次”。 它将符号墙变成您可以阅读的清单。
正则表达式设计紧凑,这使得它们强大但难以审查。 相同的模式可以用多种方式编写,单个杂散反斜杠或贪婪的量词完全改变了含义。 读取不熟悉的正则表达式 - 在代码审查、旧配置文件或堆栈溢出答案中 - 通常意味着在心理上模拟引擎。 此工具为您执行模拟,并将结果显示为有序的缩进列表,反映了模式的结构,包括嵌套组和环视。
一切都在您的代码使用的相同 JavaScript 正则表达式引擎上运行客户端,因此您在这里看到的标志行为和有效性与生产匹配。 您的模式永远不会上传,没有记录任何内容,并且一旦页面加载,该工具就会继续脱机工作。 当您试图理解的正则表达式来自私有代码库或匹配敏感数据格式时,这很重要。
说明器告诉您模式说明了什么,而不是它是否适合您的数据模式 - 检查它,根据正则表达式测试器中的实际输入运行它。 因为它是用浏览器自己的引擎编译的,所以存在于 PCRE 中但不在 ECMAScript 中的构造以两种不同的方式运行。 像 A++、原子群 (?>...) 和递归 (?r) 等所有格量词被直接拒绝,引擎自己的信息。 更微妙的情况是 \a 和 \z:没有 U 标志,JavaScript 将它们视为身份转义,因此从 PHP 或 Python 复制的模式被解释为匹配文字 A 或 Z 而不是字符串边界,并且不会引发错误。 嵌套量词(a+)+ 也被正常描述;灾难性的回溯是输入的运行时属性,而不是结构中可见的东西。
输入没有周围斜线的模式 - 只是表达式本身。 单击加载示例以从一个工作的电子邮件匹配示例开始。
切换模式使用的标志(G、I、M、S、U、Y、D)。 解释器报告每个标志的作用,并根据该标志组合验证模式。
每个令牌都按顺序列出,并带有普通英语描述。 嵌套组缩进,因此模式的结构一目了然。
复制完整的纯文本细分以粘贴到代码注释、拉取请求注释或文档中,以便下一个阅读器不必对其进行解码。
每个锚点、字符类、量词、组和转义都有自己的行,并按照引擎读取它们的顺序
捕获、非捕获、命名组和所有四种环视类型都被标记和缩进,因此嵌套结构易于遵循
G、I、M、S、U、Y、D标志均在上下文中进行描述,因此您了解它们如何改变整个匹配,而不仅仅是语法
模式使用浏览器本机正则表达式引擎验证,因此在依赖它之前捕获了一个无效的表达式,并带有确切的错误
Extract email addresses, URLs, phone numbers, and IPv4 addresses from any block of text. De-duplicate, sort, and copy the results. Runs client-side.
Find and replace text with a regular expression, using capture group backreferences like $1 and named groups, with full flag support
Test regular expressions against sample text with live match highlighting, capture groups, and full flag support
Build, test, and debug regular expressions with real-time matching and a comprehensive cheatsheet
0 comments
No comments yet. Be the first to share your thoughts!