javascript - 为什么带括号的 string.match(/(regexp)/) 返回匹配两次?

标签 javascript regex

为什么带括号的 string.match(/(regexp)/); 返回匹配两次?

例如:"abcdef".match(/(cd)/);返回 cd 的两个实例:["cd","cd"]

我查看了 MDN documentation .它没有说明多次返回匹配项。

我什至查看了 ECMA docs .

通过获取 [1] 索引 string.match() 有一个解决方法,但我在文档中找不到解释。

最佳答案

让我们按照 RegExp.match 上的文档进行操作

If the regular expression does not include the g flag, returns the same result as RegExp.exec(str).

好的,转到 RegExp.exec

If the match succeeds, the exec method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

关于javascript - 为什么带括号的 string.match(/(regexp)/) 返回匹配两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057031/

相关文章:

regex - 为什么正则表达式符号仍然用 ASCII 编写?

php - 我需要在 mongo php 中使用类似查询

r - 在 dplyr 中,是否有更简洁的方法来过滤包含大于或小于数字的列?

java - 正则表达式问题,删除整行

c# - ASP.NET MVC 5 Bootstrap Accordion 无法正常工作

javascript - 数据表函数不适用于 Reactjs

javascript - 在 require.js 中定义全新模块时使用导出是否合适?

javascript - 使用turbolinks时如何在jquery调用后更新vuejs组件数据

javascript - openCursor后两次成功

c# - 用于捕获括号中数字的正则表达式