Skip to content

regexUnusedLazyQuantifiers

Reports lazy quantifiers that have no effect because the quantifier is constant.

✅ This rule is included in the ts logical presets.

When a regular expression quantifier has fixed bounds (min equals max), there is no choice in how many times to match, so the lazy modifier ? is irrelevant. This rule reports lazy quantifiers that have no effect because the quantifier matches a fixed number of times.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">872{1<}?sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0patterAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">872{4<}?sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0patterAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">872{2,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">6E7<2{1} spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0patt446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fa spans="tyle--0:#7FDBCA;--1:#09">6E7<2{4} spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0patt446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fa spans="tyle--0:#7FDBCA;--1:#09">6E72{<2,2} spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<2}/>
AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(a72{2<}?sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0patterAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0[a-72{3<}?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">688F<(ab) spans="tyle--0:#7FDBCA;--1:#09">6E7<2{2} spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0patt446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[6E7<2{3} spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<3}/>

These are valid because the quantifier is not constant:

const pattern = /a*?/; // min=0, max=Infinity
const pattern2 = /a+?/; // min=1, max=Infinity
const pattern3 = /a??/; // min=0, max=1
const pattern4 = /a{1,3}?/; // min=1, max=3
const pattern5 = /a{2,}?/; // min=2, max=Infinity

This rule is not configurable.

If you prefer explicit lazy modifiers for documentation purposes, even when they have no effect, you might want to disable this rule.