The following statement is written in the documentation of String#match
:
if the
g
flag is not used, only the first complete match and its related capturing groups are returned. In this case, the returned item will have additional properties as described below.
Then, below the "Additional properties" subchapter is stated this:
As explained above, some results contain additional properties as described below.
groups
: An array of named capturing groups orundefined
if no named capturing groups were defined. See Groups and Ranges for more information.index
: The index of the search at which the result was found.input
: A copy of the search string.
However, when I execute the Using match()
example, the console.log
output doesn't seem to be what is expected in the snippet comments.
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.
Why the additional properties of String#match
(or RegExp#exec
, as the result is said to be same) are not in the returned result?
Is this feature planned but not implemented today?
Found out that's the SE Overflow's snippet console that doesn't output these properties.
An issue has been opened on Meta Stack Overflow.