举例说明正则表达式捕获组
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
模式的一部分可以用括号括起来 这有两个影响:
示例让我们看看在示例中的括号是如何工作的。 示例:gogogo不带括号,模式 括号将字符组合,所以 alert( 'Gogogo now!'.match(/(go)+/i) ); // "Gogogo" 示例:域名让我们做些更复杂的事 —— 搜索域名的正则表达式。 例如: mail.com
users.mail.com
smith.users.mail.com正如我们所看到的,一个域名由重复的单词组成,每个单词后面有一个点,除了最后一个单词。 在正则表达式中是 let regexp = /(\w+\.)+\w+/g; alert( "site.com my.site.com".match(regexp) ); // site.com,my.site.com 搜索有效,但该模式无法匹配带有连字符的域名,例如 my-site.com,因为连字符不属于 我们可以通过用 示例:电子邮件扩展一下上面这个示例。我们可以基于它为电子邮件创建一个正则表达式。 电子邮件的格式为: 模式: let regexp = /[-.\w]+@([\w-]+\.)+[\w-]+/g; alert("my@mail.com @ his@site.com.uk".match(regexp)); // my@mail.com, his@site.com.uk 该正则表达式并不完美的,但多数情况下都能正确匹配,并且有助于修复输入邮箱时的意外错误输入。唯一真正可靠的电子邮件检查只能通过发送电子邮件来完成。 匹配中的括号的内容括号被从左到右编号。正则引擎会记住它们各自匹配的内容,并允许在结果中获取它。 方法
例如,我们想找到 HTML 标签 让我们将内部内容包装在括号中,像这样: 现在,我们在结果数组中得到了标签的整体 let str = '<h1>Hello, world!</h1>'; let tag = str.match(/<(.*?)>/); alert( tag[0] ); // <h1> alert( tag[1] ); // h1 嵌套组括号可以嵌套。在这种情况下,编号也从左到右。 例如,在搜索标签
让我们为它们添加括号: 这是它们的编号方式(根据左括号从左到右):
验证: let str = '<span class="my">'; let regexp = /<(([a-z]+)\s*([^>]*))>/; let result = str.match(regexp); alert(result[0]); // <span class="my"> alert(result[1]); // span class="my" alert(result[2]); // span alert(result[3]); // class="my"
然后是按左括号从左到右编号的组。第一组返回为 然后是 字符串中每个组的内容:
可选组即使组是可选的并且在匹配项中不存在(例如,具有量词 例如,让我们考虑正则表达式 如果我们在单个字母的字符串上运行 let match = 'a'.match(/a(z)?(c)?/); alert( match.length ); // 3 alert( match[0] ); // a(完整的匹配项) alert( match[1] ); // undefined alert( match[2] ); // undefined 数组的长度为 对字符串 let match = 'ac'.match(/a(z)?(c)?/) alert( match.length ); // 3 alert( match[0] ); // ac(完整的匹配项) alert( match[1] ); // undefined, 因为没有 (z)? 的匹配项 alert( match[2] ); // c 数组长度依然是: 带有组搜索所有匹配项:matchAll
当我们搜索所有匹配项(修饰符 例如,让我们查找字符串中的所有标签: let str = '<h1> <h2>'; let tags = str.match(/<(.*?)>/g); alert( tags ); // <h1>,<h2> 结果是一个匹配数组,但没有每个匹配项的详细信息。但是实际上,我们通常需要在结果中获取捕获组的内容。 要获取它们,我们应该使用方法 在使用 就像
例如: let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi); // results —— 不是数组,而是一个迭代对象 alert(results); // [object RegExp String Iterator] alert(results[0]); // undefined (*) results = Array.from(results); // 让我们将其转换为数组 alert(results[0]); // <h1>,h1(第一个标签) alert(results[1]); // <h2>,h2(第二个标签) 我们可以看到,第一个区别非常重要,如 如果我们只需要遍历结果,则 let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi); for(let result of results) { alert(result); // 第一个 alert:<h1>,h1 // 第二个:<h2>,h2 } ……或使用解构: let [tag1, tag2] = '<h1> <h2>'.matchAll(/<(.*?)>/gi);
let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi); let [tag1, tag2] = results; alert( tag1[0] ); // <h1> alert( tag1[1] ); // h1 alert( tag1.index ); // 0 alert( tag1.input ); // <h1> <h2>
命名组用数字记录组很困难。对于简单的模式,它是可行的,但对于更复杂的模式,计算括号很不方便。我们有一个更好的选择:给括号命名。 在左括号后紧跟着放置 例如,让我们查找 “year-month-day” 格式的日期: let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/; let str = "2019-04-30"; let groups = str.match(dateRegexp).groups; alert(groups.year); // 2019 alert(groups.month); // 04 alert(groups.day); // 30 正如你所看到的,匹配的组在 要查找所有日期,我们可以添加修饰符 我们还需要 let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g; let str = "2019-10-30 2020-01-01"; let results = str.matchAll(dateRegexp); for(let result of results) { let {year, month, day} = result.groups; alert(`${day}.${month}.${year}`); // 第一个 alert:30.10.2019 // 第二个:01.01.2020 } 替换中的捕获组让我们能够替换 例如, let str = "John Bull"; let regexp = /(\w+) (\w+)/; alert( str.replace(regexp, '$2, $1') ); // Bull, John 对于命名的括号,引用为 例如,让我们将日期格式从 “year-month-day” 更改为 “day.month.year”: let regexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g; let str = "2019-10-30, 2020-01-01"; alert( str.replace(regexp, '$<day>.$<month>.$<year>') ); // 30.10.2019, 01.01.2020 非捕获组 ?:有时我们需要用括号才能正确应用量词,但我们不希望它们的内容出现在结果中。 可以通过在开头添加 例如,如果我们要查找 在下面的示例中,我们仅将名称 let str = "Gogogo John!"; // ?: 从捕获组中排除 'go' let regexp = /(?:go)+ (\w+)/i; let result = str.match(regexp); alert( result[0] ); // Gogogo John(完整的匹配项) alert( result[1] ); // John alert( result.length ); // 2(在数组中没有其他数组项) 总结括号将正则表达式中的一部分组合在一起,以便量词可以整体应用。 括号组从左到右编号,可以选择用 可以在结果中获得按组匹配的内容:
如果括号没有名称,则匹配数组按编号提供其内容。命名括号还可使用属性 我们还可以在 可以通过在组的开头添加 转自https://www.cnblogs.com/lxlx1798/articles/16976622.html 该文章在 2025/12/4 9:00:40 编辑过 |
关键字查询
相关文章
正在查询... |