regexZeroQuantifiers
Reports quantifiers with a maximum of zero, which are useless.
✅ This rule is included in the ts logical presets.
Reports quantifiers with a maximum of zero ({0} or {0,0}), which are useless because they match zero occurrences (the empty string).
A quantifier with a maximum of 0 means the quantified element will never be matched, so both the quantifier and its element can be removed.
Examples
Section titled “Examples”Simple Element with Zero Quantifier
Section titled “Simple Element with Zero Quantifier”const pattern = /a{0}/;const pattern = /a?/;The {0} quantifier means a will never be matched, making the entire quantified expression useless.
Zero Range Quantifier
Section titled “Zero Range Quantifier”const pattern = /a{0,0}/;const pattern = /a{0,1}/;The {0,0} quantifier also means exactly zero matches, making it equivalent to an empty match.
Group with Zero Quantifier
Section titled “Group with Zero Quantifier”const pattern = /(ab){0}/;const pattern = /(ab)?/;The group (ab) with {0} will never match anything useful.
Character Class with Zero Quantifier
Section titled “Character Class with Zero Quantifier”const pattern = /[a-z]{0}/;const pattern = /[a-z]*/;The character class [a-z] with {0} will never match any character.
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”There is no valid use case for a quantifier with a maximum of 0, so this rule should generally always be enabled.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-zero-quantifier