Skip to content

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.

const pattern = /(a?){5}/;

The {5} suggests exactly 5 matches are required, but since a? can match empty, the pattern can match the empty string.

const pattern = /(?:a?b*|c+){4}/;

The {4} suggests at least 4 matches, but the a?b* alternative can match empty.

const pattern = /(a?)+/;

The + suggests at least 1 match is required, but a? can match empty.

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.