以下语句写在the documentation of String#match
中:
如果未使用
g
标志,则仅返回第一个完整匹配及其相关捕获组。在这种情况下,返回的商品将具有如下所述的其他属性。
然后,在“ 其他属性”子章下面指出:
如上所述,某些结果包含如下所述的其他属性。
groups
:命名捕获组的数组;如果未定义命名捕获组,则为undefined
。有关更多信息,请参见组和范围。index
:找到结果的搜索索引。input
:搜索字符串的副本。
但是,当我执行Using match()
example时,console.log
的输出似乎与摘要注释中所期望的不一样。
var str = 'For more information, see Chapter 3.4.5.1';
var re = /see (chapter \d+(\.\d)*)/i;
var found = str.match(re);
console.log(found);
// logs [ 'see Chapter 3.4.5.1',
// 'Chapter 3.4.5.1',
// '.1',
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1' ]
// 'see Chapter 3.4.5.1' is the whole match.
// 'Chapter 3.4.5.1' was captured by '(chapter \d+(\.\d)*)'.
// '.1' was the last value captured by '(\.\d)'.
// The 'index' property (22) is the zero-based index of the whole match.
// The 'input' property is the original string that was parsed.
为什么String#match
(或RegExp#exec
,因为据说结果相同)的附加属性不在返回的结果中?
此功能是否已计划但今天尚未实现?
发现这是SE Overflow的代码段控制台,不会输出这些属性。
问题已在Meta Stack Overflow上发布。