Skip to content

regexUnnecessaryNestedQuantifiers

Reports trivially nested quantifiers in regular expressions that can be simplified.

✅ This rule is included in the ts logical presets.

When a non-capturing group contains a single quantified element and the group itself is quantified, the nested quantifiers can often be combined into a single quantifier. This rule reports trivially nested quantifiers in regular expressions that can be simplified.

(?:a?)+ can be simplified to a* because an optional element repeated one or more times is equivalent to zero or more.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(?<:asp ansty="le--0:#7FDBCA;--1:#096E">7<2?sp ansty="le--0:#5CA7E4;--1:#3A68">87<2+sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fa spans="tyle--0:#7FDBCA;--1:#09">61<111/ spans="tyle--0:#D6DEEB;--1:#40">3

(?:a+)+ can be simplified to a+ because one or more of one or more is still one or more.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(?<:asp ansty="le--0:#7FDBCA;--1:#096E">7<2+sp ansty="le--0:#5CA7E4;--1:#3A68">87<2+sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fa spans="tyle--0:#7FDBCA;--1:#09">61<111/ spans="tyle--0:#D6DEEB;--1:#40">3

(?:a+)* can be simplified to a* because one or more repeated zero or more times is zero or more.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(?<:asp ansty="le--0:#7FDBCA;--1:#096E">7<2+sp ansty="le--0:#5CA7E4;--1:#3A68">87<2*sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fa spans="tyle--0:#7FDBCA;--1:#09">61<111/ spans="tyle--0:#D6DEEB;--1:#40">3

The rule also works with character classes and escape sequences.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(B0[a-7<2?sp ansty="le--0:#5CA7E4;--1:#3A68">87<2+sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0digiAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(B0<\dsp ansty="le--0:#7FDBCA;--1:#096E">7<2+sp ansty="le--0:#5CA7E4;--1:#3A68">87<2+sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[61<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0di446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">6161<111/ spans="tyle--0:#D6DEEB;--1:#40">3
AEconsB0patteAE <= sp ansty="le--0:#7FDBCA;--1:#096E">72nAB0RegE5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4D(?:a?<)+sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEco61B0pat44A6E7<2new spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#82AAFF;--1:#3B">61B0Re31<111" spans="tyle--0:#ECC48D;--1:#98">4E<4Da* spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name"><*")>

This rule is not configurable.

If you prefer to keep the nested structure for readability or documentation purposes, or if you need the specific behavior of the non-capturing group for replacement operations, you might prefer to disable this rule. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.