regexMisleadingQuantifiers
Reports quantifiers whose minimum implies they must match but whose element can match empty.
✅ This rule is included in the ts logical presets.
Reports quantifiers whose minimum implies they must match but whose element can match empty. These quantifiers are misleading because they suggest a minimum number of matches, but the quantified element can match the empty string, making the effective minimum 0.
Examples
Section titled “Examples”Optional Element with Fixed Repetition
Section titled “Optional Element with Fixed Repetition”const pattern = /(a?){5}/;const pattern = /(a?){0,5}/;The {5} suggests exactly 5 matches are required, but since a? can match empty, the pattern can match the empty string.
Alternation with Optional Elements
Section titled “Alternation with Optional Elements”const pattern = /(?:a?b*|c+){4}/;const pattern = /(?:a?b*|c+){0,4}/;The {4} suggests at least 4 matches, but the a?b* alternative can match empty.
Plus Quantifier on Empty-Matchable Group
Section titled “Plus Quantifier on Empty-Matchable Group”const pattern = /(a?)+/;const pattern = /(a?)*/;The + suggests at least 1 match is required, but a? can match empty.
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you use misleading quantifiers intentionally for backtracking behavior, or if you use quantifiers to control iteration count rather than match content, you might want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/confusing-quantifier