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.

AEconsB0regAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(B0f8F>b<)csp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0matAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53regAB0ex53(text<);d ivclassNa="meec-li">AE53 (match)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0valAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEco61B0r446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F61B<0foo spans="tyle--0:#5CA7E4;--1:#3A">688F<>b)c spans="tyle--0:#D9F5DD;--1:#11">1b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0m44A3F53r4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53(te443F53 (matcde44AEco61B0v44A3F53m4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E7<2foo spans="tyle--0:#D6DEEB;--1:#40">33
AEconsB0resuAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1<1"sp ansty="le--0:#C792EA;--1:#8844">AB0mat5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(B0f8F>b<)csp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meec-li">AE53 (result)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0valAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53resul8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEco61B0re44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#C792EA;--1:#88">4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0m31<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F61B<0foo spans="tyle--0:#5CA7E4;--1:#3A">688F<>b)c spans="tyle--0:#D9F5DD;--1:#11">1b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">443F53 (resulde44AEco61B0v44A3F53re4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E7<2foo spans="tyle--0:#D6DEEB;--1:#40">33
AEconsB0matchAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1<1"sp ansty="le--0:#C792EA;--1:#8844">AB0matchA5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(B0f8F>b<)csp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">853<);d ivclassNa="meec-li">AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconsB0matA7253 matches)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0valAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEco61B0mat44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#C792EA;--1:#88">4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0matc31<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F61B<0foo spans="tyle--0:#5CA7E4;--1:#3A">688F<>b)c spans="tyle--0:#D9F5DD;--1:#11">1b)c><111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fg spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B0m4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72of spans="tyle--0:#D6DEEB;--1:#40">3F53 matchede44AEco61B0v44A3F53m4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E7<2foo spans="tyle--0:#D6DEEB;--1:#40">33

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

AEconsB0regAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(a)(B0b8F>b<)csp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0matAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53regAB0ex53(text<);d ivclassNa="meec-li">AE53 (match)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0firAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0secoAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEco61B0r446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(a<)(?< spans="tyle--0:#C5E478;--1:#3B">61B<0bar spans="tyle--0:#5CA7E4;--1:#3A">688F<>b)c spans="tyle--0:#D9F5DD;--1:#11">1b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0m44A3F53r4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53(te443F53 (matcde44AEco61B0f44A3F53ma0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">de44AEco61B0se44A3F53m4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E7<2bar spans="tyle--0:#D6DEEB;--1:#40">33

Using named groups correctly:

const regex = /a(?<foo>b)c/;
const match = regex.exec(text);
if (match) {
const value = match.groups.foo;
}

Accessing unnamed capturing groups by index:

const regex = /a(b)c/;
const match = regex.exec(text);
if (match) {
const value = match[1];
}

Accessing match index 0 (the full match):

const regex = /a(?<foo>b)c/;
const match = regex.exec(text);
if (match) {
const fullMatch = 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.