Skip to content

regexResultArrayGroups

Reports indexed access on regex result arrays when named capturing groups should be used.

✅ This rule is included in the ts stylisticStrict presets.

When a regular expression has named capturing groups, accessing the matched values through .groups.name is more readable and maintainable than using numeric indices like result[1]. Numeric indices are fragile because they can break if the regex pattern is modified (e.g., adding a new capturing group before the one you’re accessing).

This rule reports indexed access on regex result arrays when the index corresponds to a named capturing group.

const
const regex: RegExp
regex
= /a(?<foo>b)c/;
const
const match: RegExpExecArray | null
match
=
const regex: RegExp
regex
.
RegExp.exec(string: string): RegExpExecArray | null

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

@paramstring The String object or string literal on which to perform the search.

exec
(
const text: any
text
);
if (
const match: RegExpExecArray | null
match
) {
const
const value: string
value
=
const match: RegExpExecArray
match
[1];
}
const
const result: RegExpMatchArray | null
result
= "text".
String.match(matcher: {
[Symbol.match](string: string): RegExpMatchArray | null;
}): RegExpMatchArray | null (+1 overload)

Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.

@parammatcher An object that supports being matched against.

match
(/a(?<foo>b)c/);
if (
const result: RegExpMatchArray | null
result
) {
const
const value: string
value
=
const result: RegExpMatchArray
result
[1];
}
const
const matches: RegExpStringIterator<RegExpExecArray>
matches
= "text".
String.matchAll(regexp: RegExp): RegExpStringIterator<RegExpExecArray>

Matches a string with a regular expression, and returns an iterable of matches containing the results of that search.

@paramregexp A variable name or string literal containing the regular expression pattern and flags.

matchAll
(/a(?<foo>b)c/g);
for (const
const match: RegExpExecArray
match
of
const matches: RegExpStringIterator<RegExpExecArray>
matches
) {
const
const value: string
value
=
const match: RegExpExecArray
match
[1];
}

When a regex has both named and unnamed capturing groups, only access to the named groups is reported:

const
const regex: RegExp
regex
= /(a)(?<bar>b)c/;
const
const match: RegExpExecArray | null
match
=
const regex: RegExp
regex
.
RegExp.exec(string: string): RegExpExecArray | null

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

@paramstring The String object or string literal on which to perform the search.

exec
(
const text: any
text
);
if (
const match: RegExpExecArray | null
match
) {
const
const first: string
first
=
const match: RegExpExecArray
match
[1];
const
const second: string
second
=
const match: RegExpExecArray
match
[2];
}

Using named groups correctly:

const
const regex: RegExp
regex
= /a(?<foo>b)c/;
const
const match: RegExpExecArray | null
match
=
const regex: RegExp
regex
.
RegExp.exec(string: string): RegExpExecArray | null

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

@paramstring The String object or string literal on which to perform the search.

exec
(
const text: any
text
);
if (
const match: RegExpExecArray | null
match
) {
const
const value: string
value
=
const match: RegExpExecArray
match
.
RegExpExecArray.groups?: {
[key: string]: string;
} | undefined
groups
.
string
foo
;
}

Accessing unnamed capturing groups by index:

const
const regex: RegExp
regex
= /a(b)c/;
const
const match: RegExpExecArray | null
match
=
const regex: RegExp
regex
.
RegExp.exec(string: string): RegExpExecArray | null

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

@paramstring The String object or string literal on which to perform the search.

exec
(
const text: any
text
);
if (
const match: RegExpExecArray | null
match
) {
const
const value: string
value
=
const match: RegExpExecArray
match
[1];
}

Accessing match index 0 (the full match):

const
const regex: RegExp
regex
= /a(?<foo>b)c/;
const
const match: RegExpExecArray | null
match
=
const regex: RegExp
regex
.
RegExp.exec(string: string): RegExpExecArray | null

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

@paramstring The String object or string literal on which to perform the search.

exec
(
const text: any
text
);
if (
const match: RegExpExecArray | null
match
) {
const
const fullMatch: string
fullMatch
=
const match: RegExpExecArray
match
[0];
}

This rule is not configurable.

If you have a codebase that consistently uses numeric indices for all capturing groups, migrating to named groups may require significant refactoring. You might also prefer to disable this rule if you prefer the brevity of numeric indices over the clarity of named groups.

Made with ❤️‍🔥 around the world by the Flint team and contributors.