This proposal adds a new `.indices` property to the result array object returned by RegExp.prototype.exec and String.prototype.match. This property is an indices array containing a pair of start and end indices for each captured substring. As producing this array is expensive, the `.indices` property is only present when the /d flag is passed.

Motivation

The result Array from RegExp.prototype.exec contains information about the substrings that were matched by the regular expression. While it already contains the input string, the index at which the pattern matched, and a groups object containing the substrings for any named capture groups, this information may be insufficient for advanced scenarios like reporting errors that highlight the offending substring. This feature bridges the gap by providing a `.indices` property, an Array that contains a pair of start and end indices for each captured substring.

Specification

Specification link


Specification being incubated in a Community Group

Status in Chromium

Blink


Enabled by default (tracking bug)

Consensus & Standardization

After a feature ships in Chrome, the values listed here are not guaranteed to be up to date.

Owner

Last updated on 2021-06-09