Reports capturing groups in regular expressions that do not have a name.
✅ This rule is included in the ts preset.
Named capture groups ((?<name>...)) provide several advantages over numbered capture groups:
- They make regex patterns more self-documenting
- They are easier to reference in replacement strings
- They don’t break when the order of groups changes
- They work better with destructuring in match results
If a capture is not actually needed, consider using a non-capturing group (?:...) instead.
This rule reports capturing groups in regular expressions that do not have a name.
AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">8 =sp>1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">81/sp>B0[0-<9]sp ansty="le--0:#7FDBCA;--1:#096E">72{9]sp><4}sp ansty="le--0:#5CA7E4;--1:#3A68">8F)4}sp><-(sp ansty="le--0:#82AAFF;--1:#3B61">B0[0--(sp><9]sp ansty="le--0:#7FDBCA;--1:#096E">72{9]sp><2}sp ansty="le--0:#5CA7E4;--1:#3A68">8F)2}sp><-(sp ansty="le--0:#82AAFF;--1:#3B61">B0[0--(sp><9]sp ansty="le--0:#7FDBCA;--1:#096E">72{9]sp><2}sp ansty="le--0:#5CA7E4;--1:#3A68">82}sp>1<1/sp ansty="le--0:#D6DEEB;--1:#403F">51/sp><3;d ivclassNa="meco">;d>/;d>3;d>AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">8 =sp>1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(foo)(ba1/sp>1<1/sp ansty="le--0:#D6DEEB;--1:#403F">51/sp><3;d ivclassNa="meco">;d>/;d>3;d> 44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">188F ><111/ spans="tyle--0:#5CA7E4;--1:#3A">688111/>61B06<88F> spans="tyle--0:#82AAFF;--1:#3B">61B0[88F>><0-9] spans="tyle--0:#7FDBCA;--1:#09">6E70-9]><2{4} spans="tyle--0:#5CA7E4;--1:#3A">688F)2{4}><-(?< spans="tyle--0:#C5E478;--1:#3B">61B0m-(?<>6<88F> spans="tyle--0:#82AAFF;--1:#3B">61B0[88F>><0-9] spans="tyle--0:#7FDBCA;--1:#09">6E70-9]><2{2} spans="tyle--0:#5CA7E4;--1:#3A">688F)2{2}><-(?< spans="tyle--0:#C5E478;--1:#3B">61B-(?<><0day spans="tyle--0:#5CA7E4;--1:#3A">60day><88F> spans="tyle--0:#82AAFF;--1:#3B">61B0[88F>><0-9] spans="tyle--0:#7FDBCA;--1:#09">6E70-9]><2{2} spans="tyle--0:#5CA7E4;--1:#3A">62{2}><88F) spans="tyle--0:#D9F5DD;--1:#11">188F)><111/ spans="tyle--0:#D6DEEB;--1:#40">3111/>[0-9]{4})-(?[0-9]{2})-(?[0-9]{2"><})/>})/><;di vdivclass="Nameexpressive-">6376// Non-capturing groups when capture is not ne44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">188F ><111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(?:foo)(?:111/>1<111/ spans="tyle--0:#D6DEEB;--1:#40">3111/>;di><;di vdivclass="Nameexpressive-">6376// Mix of named and non-capturing gr44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">188F ><111/ spans="tyle--0:#5CA7E4;--1:#3A">688111/>61B0prot688F>h668<8F): spans="tyle--0:#F78C6C;--1:#AA">09828F):><\/\/ spans="tyle--0:#5CA7E4;--1:#3A">688F(?\/\/><:www spans="tyle--0:#F78C6C;--1:#AA">09<82\. spans="tyle--0:#5CA7E4;--1:#3A">682\.><88F) spans="tyle--0:#7FDBCA;--1:#09">688F)>68861B0do6<88F> spans="tyle--0:#82AAFF;--1:#3B">688F>><1B0[ spans="tyle--0:#7FDBCA;--1:#09">61B0[>6166<88F) spans="tyle--0:#D9F5DD;--1:#11">188F)><111/ spans="tyle--0:#D6DEEB;--1:#40">3111/>https?):\/\/(?:www\.)?(?[^/]"><+)/>+)/>;di>
This rule is not configurable.
If you have a large codebase with many existing unnamed capture groups that would be difficult to refactor, you might prefer to disable this rule.
Some developers also find numbered groups sufficient for simple regex patterns with few groups.