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.
AEcons B0reg AE < =sp ansty="le--0:#5CA7E4;--1:#3A68">8 =sp>1 <1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(1/sp>B0f?8F>b <)csp ansty="le--0:#D9F5DD;--1:#1111">1)csp><1/sp ansty="le--0:#D6DEEB;--1:#403F">51/sp> <3;d ivclassNa="meec-li">AEcons B0mat AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53reg= sp>A B0ex 53(text 3;d><);d ivclassNa="meec-li">AE 53 (match) );d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEcons sp>B0val AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc= sp>8 <21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp> {d><];d ivclassNa="meec-li">5 ];d> <3}d ivclassNa="meco">b)c/;const match = regex.exec(text);if (match) { const value = match[1];"><}d>}d> 3}d>44AEco61B0r 44 6 <88F spans="tyle--0:#D9F5DD;--1:#11">188F ><111/ spans="tyle--0:#5CA7E4;--1:#3A">688F111/>61B <0foo spans="tyle--0:#5CA7E4;--1:#3A">688F0foo><>b)c spans="tyle--0:#D9F5DD;--1:#11">1>b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">3111/>44AEco61B0m 44A 3F53r 4 <4AE. spans="tyle--0:#82AAFF;--1:#3B">61B04AE.>3F53(te 443F53 (matc de44AEco 61B0v 44A 3F53m 4 <4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4AE.>4 <4AE. spans="tyle--0:#7FDBCA;--1:#09">6E74AE.><2foo spans="tyle--0:#D6DEEB;--1:#40">32foo>3b)c/;const match = regex.exec(text);if (match) { const value = match.groups.fo">
AEcons B0resu AE <= sp ansty="le--0:#D9F5DD;--1:#1111">1= sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1"sp>1 <1"sp ansty="le--0:#C792EA;--1:#8844">A1"sp>B0mat 5 <3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(1/sp>B0f?8F>b <)csp ansty="le--0:#D9F5DD;--1:#1111">1)csp><1/sp ansty="le--0:#D6DEEB;--1:#403F">531/sp> <);d ivclassNa="meec-li">AE 53 (result) );d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEcons sp>B0val AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53resul= sp>8 <21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp> {d><];d ivclassNa="meec-li">5 ];d> <3}d ivclassNa="meco">b)c/);if (result) { const value = result[1];"><}d>}d> 3}d>44AEco61B0re 44A 1 <111" spans="tyle--0:#ECC48D;--1:#98">4E4D111">1 <111" spans="tyle--0:#C792EA;--1:#88">4111"><4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0m4AE.>3 1 <111/ spans="tyle--0:#5CA7E4;--1:#3A">688F111/>61B <0foo spans="tyle--0:#5CA7E4;--1:#3A">688F0foo><>b)c spans="tyle--0:#D9F5DD;--1:#11">1>b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">3F111/> <53) ;divclass="Nameec-">443F53 (resul 53)>de44AEco 61B0v 44A 3F53re 4 <4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4AE.>4 <4AE. spans="tyle--0:#7FDBCA;--1:#09">6E74AE.><2foo spans="tyle--0:#D6DEEB;--1:#40">32foo>3b)c/);if (result) { const value = result.groups.fo">
AEcons B0match AE <= sp ansty="le--0:#D9F5DD;--1:#1111">1= sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1"sp>1 <1"sp ansty="le--0:#C792EA;--1:#8844">A1"sp>B0matchA 5 <3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1/sp ansty="le--0:#5CA7E4;--1:#3A68">8Fa(1/sp>B0f?8F>b <)csp ansty="le--0:#D9F5DD;--1:#1111">1)csp><1/sp ansty="le--0:#5CA7E4;--1:#3A68">81/sp>53 <);d ivclassNa="meec-li">AEf 53 < (sp ansty="le--0:#C792EA;--1:#8844">AEcons (sp>B0mat A 72 53 matches) );d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEcons sp>B0val AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc= sp>8 <21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp> {d><];d ivclassNa="meec-li">5 ];d> <3}d ivclassNa="meco">b)c/g);for (const match of matches) { const value = match[1];"><}d>}d> 3}d>44AEco61B0mat 44A 1 <111" spans="tyle--0:#ECC48D;--1:#98">4E4D111">1 <111" spans="tyle--0:#C792EA;--1:#88">4111"><4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0matc4AE.>3 1 <111/ spans="tyle--0:#5CA7E4;--1:#3A">688F111/>61B <0foo spans="tyle--0:#5CA7E4;--1:#3A">688F0foo><>b)c spans="tyle--0:#D9F5DD;--1:#11">1>b)c><111/ spans="tyle--0:#5CA7E4;--1:#3A">6111/><88Fg spans="tyle--0:#D6DEEB;--1:#40">3F88Fg> <53) ;divclass="Nameec-">44A3F <53 ( spans="tyle--0:#C792EA;--1:#88">44AEco53 (>61B0m 4 <4AE spans="tyle--0:#7FDBCA;--1:#09">6E4AE ><72of spans="tyle--0:#D6DEEB;--1:#40">3F53 matche72of> 53)>de44AEco 61B0v 44A 3F53m 4 <4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4AE.>4 <4AE. spans="tyle--0:#7FDBCA;--1:#09">6E74AE.><2foo spans="tyle--0:#D6DEEB;--1:#40">32foo>3b)c/g);for (const match of matches) { const value = match.groups.fo">
When a regex has both named and unnamed capturing groups, only access to the named groups is reported:
AEcons B0reg AE < =sp ansty="le--0:#5CA7E4;--1:#3A68">8 =sp>1 <1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(a)(1/sp>B0b?8F>b <)csp ansty="le--0:#D9F5DD;--1:#1111">1)csp><1/sp ansty="le--0:#D6DEEB;--1:#403F">51/sp> <3;d ivclassNa="meec-li">AEcons B0mat AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53reg= sp>A B0ex 53(text 3;d><);d ivclassNa="meec-li">AE 53 (match) );d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEcons sp>B0fir AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc= sp>8 <21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp> {d><];d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEcons sp>B0seco AE <= sp ansty="le--0:#D6DEEB;--1:#403F">53matc= sp>8 <22sp ansty="le--0:#D6DEEB;--1:#403F">5322sp> ];d><];d ivclassNa="meec-li">5 ];d> <3}d ivclassNa="meco">b)c/;const match = regex.exec(text);if (match) { const first = match[1]; const second = match[2];"><}d>}d> 3}d>44AEco61B0r 44 6 <88F spans="tyle--0:#D9F5DD;--1:#11">188F ><111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(a111/><)(?< spans="tyle--0:#C5E478;--1:#3B">61B)(?<><0bar spans="tyle--0:#5CA7E4;--1:#3A">688F0bar><>b)c spans="tyle--0:#D9F5DD;--1:#11">1>b)c><111/ spans="tyle--0:#D6DEEB;--1:#40">3111/>44AEco61B0m 44A 3F53r 4 <4AE. spans="tyle--0:#82AAFF;--1:#3B">61B04AE.>3F53(te 443F53 (matc de44AEco 61B0f 44A 3F53ma 0 <9821 spans="tyle--0:#D6DEEB;--1:#40">3F9821> <53] ;divclass="Nameec-">de44AEco 61B0se 44A 3F53m 4 <4AE. spans="tyle--0:#FAF39F;--1:#11">1111gr4AE.>4 <4AE. spans="tyle--0:#7FDBCA;--1:#09">6E74AE.><2bar spans="tyle--0:#D6DEEB;--1:#40">32bar> 53]>3b)c/;const match = regex.exec(text);if (match) { const first = match[1]; const second = match.groups.ba">
Using named groups correctly:
const regex = / a(?< foo >b)c / ;
const match = regex . exec (text);
const value = match . groups . foo ;
Accessing unnamed capturing groups by index:
const match = regex . exec (text);
Accessing match index 0 (the full match):
const regex = / a(?< foo >b)c / ;
const match = regex . exec (text);
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.