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.
Examples
Section titled “Examples”Fixed Count Quantifiers
Section titled “Fixed Count Quantifiers”const pattern = /a{1}?/;const pattern2 = /a{4}?/;const pattern3 = /a{2,2}?/;const pattern = /a{1}/;const pattern2 = /a{4}/;const pattern3 = /a{2,2}/;Groups and Character Classes
Section titled “Groups and Character Classes”const pattern = /(ab){2}?/;const pattern2 = /[a-z]{3}?/;const pattern = /(ab){2}/;const pattern2 = /[a-z]{3}/;Variable Quantifiers (Valid)
Section titled “Variable Quantifiers (Valid)”These are valid because the quantifier is not constant:
const pattern = /a*?/; // min=0, max=Infinityconst pattern2 = /a+?/; // min=1, max=Infinityconst pattern3 = /a??/; // min=0, max=1const pattern4 = /a{1,3}?/; // min=1, max=3const pattern5 = /a{2,}?/; // min=2, max=InfinityOptions
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer explicit lazy modifiers for documentation purposes, even when they have no effect, 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/no-useless-lazy
Made with ❤️🔥 around the world by
the Flint team and contributors.