CKTIS / JavaScript
RegExp match results: `index` and `groups`
Read where the full match starts and what each named group captured. Use the `d` flag when you also need start and end offsets.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const m = re.exec(text); // Match array or null
// After checking m !== null:
m.index; // Start of the full match
m[0]; // Full matched text
m[1]; // First capture, if present
m.groups?.name; // Text from (?<name>...) if it matched
m.indices?.[0]; // Full match range with the d flag
m.indices?.groups?.name; // Named capture range with the d flagre.exec(text) returns a match array or null if there is no match. Check for null before reading properties. In the array m, m[0] is the full match, and m[1] onward contain captures, including named ones, in opening-parenthesis order. m.input is the entire input string.
m.index is the zero-based start of the full match in UTF-16 code units, even with the u flag. It is not a capture number or re.lastIndex: with g or y, the regex stores in lastIndex the position from which the next exec() call starts.
A group written as (?<name>...) exposes its captured text as m.groups.name. With no named groups, m.groups is undefined; otherwise it is an object with no prototype. A group that did not participate in the match has the value undefined, not the empty string "".
The d flag adds m.indices: m.indices[0] is the full match range, m.indices[1] is the first capture's range, and m.indices.groups.name is the named capture's range. Each available [start, end] pair uses UTF-16 offsets and excludes end, so it works directly with text.slice(start, end). A group that did not participate has undefined instead of a pair; without d, the m.indices property is absent. Check support for d and named groups in your target runtime.
With a standard RegExp, text.match(re) without g gives the same kind of result as exec(). With g, it returns only the full matched strings or null, with no per-match metadata. To get that metadata, iterate over text.matchAll(re) with g. The iterator uses a copy of the regex initialized with the current re.lastIndex, so reusing a regex with a nonzero lastIndex can skip earlier matches. To search from the beginning, use a fresh regex or set re.lastIndex = 0 before calling matchAll(). Passing a RegExp without g throws a TypeError.
Examples
(() => {
const text = "😀 id=42";
const m = /id=(?<id>\d+)(?<unit>ms)?/du.exec(text);
if (m === null) throw new Error("Expected a match");
const [start, end] = m.indices.groups.id;
console.log(JSON.stringify({
match: m[0],
index: m.index,
id: m.groups.id,
idRange: [start, end],
slicedId: text.slice(start, end),
unitMissing: m.groups.unit === undefined
}));
})();Result
{"match":"id=42","index":3,"id":"42","idRange":[6,8],"slicedId":"42","unitMissing":true}The emoji takes two UTF-16 code units and the space takes one, so `id=42` starts at `3`. The `id` group captures the string `"42"` from `[6, 8]`; the optional `unit` group does not participate.
Related lessons
- JavaScript · Lesson 25 · Intermediate · Version: 3