Regular expression capturing group offsets
ECMAScript proposal for offset information for capturing groups in regular expressions.
Rationale
ECMAScript allows to match a regular expression against a string, which returns the matched chunks. For further processing you sometimes additionally require the offsets of the capturing groups of those matches, which are not exposed by the current APIs.
Proposed solution
The array returned by RegExp.prototype.exec()
should be extended by an offsets
property, which represents an array of the start indices of the match and all the captured groups.
Examples
Non-global regular expression
let matches = /a(b(c))/.exec("abcabc");
Then matches
will contain this:
{
0: "abc",
1: "bc",
2: "c",
index: 0,
input: "abcabc",
offsets: [
0,
1
2
]
}
Global regular expression
let matches = /a(b(c))/g.exec("abcabc");
Then matches
will contain this when executed the first time:
{
0: "abc",
1: "bc",
2: "c",
index: 0,
input: "abcabc",
offsets: [
0,
1
2
]
}
And this when executed the second time:
{
0: "abc",
1: "bc",
2: "c",
index: 3,
input: "abcabc",
offsets: [
3,
4
5
]
}