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.
Examples
Section titled “Examples”RegExp.prototype.exec()
Section titled “RegExp.prototype.exec()”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.
exec(const text: any
text);if (const match: RegExpExecArray | null
match) { const const value: string
value = const match: RegExpExecArray
match[1];}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.
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;}String.prototype.match()
Section titled “String.prototype.match()”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.
match(/a(?<foo>b)c/);if (const result: RegExpMatchArray | null
result) { const const value: string
value = const result: RegExpMatchArray
result[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.
match(/a(?<foo>b)c/);if (const result: RegExpMatchArray | null
result) { const const value: string
value = const result: RegExpMatchArray
result.RegExpMatchArray.groups?: { [key: string]: string;} | undefined
groups.string
foo;}String.prototype.matchAll()
Section titled “String.prototype.matchAll()”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.
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];}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.
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.RegExpExecArray.groups?: { [key: string]: string;} | undefined
groups.string
foo;}Mixed Named and Unnamed Groups
Section titled “Mixed Named and Unnamed Groups”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.
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];}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.
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.RegExpExecArray.groups?: { [key: string]: string;} | undefined
groups.string
bar;}Valid Uses
Section titled “Valid Uses”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.
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.
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.
exec(const text: any
text);if (const match: RegExpExecArray | null
match) { const const fullMatch: string
fullMatch = const match: RegExpExecArray
match[0];}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.