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.

const pattern = /a{1}?/;
const pattern2 = /a{4}?/;
const pattern3 = /a{2,2}?/;
const pattern = /(ab){2}?/;
const pattern2 = /[a-z]{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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.