pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/no-useless-quantifier |
disallow quantifiers that can be removed |
v0.10.0 |
💼 This rule is enabled in the ✅ plugin:regexp/recommended
config.
🔧💡 This rule is automatically fixable by the --fix
CLI option and manually fixable by editor suggestions.
disallow quantifiers that can be removed
This rule reports quantifiers that can trivially be removed without affecting the pattern.
This rule only fixes constant-one quantifiers (e.g. a{1}
). All other reported useless quantifiers hint at programmer oversight or fundamental problems with the pattern.
Examples:
-
a{1}
It's clear that the
{1}
quantifier can be removed. -
(?:a+b*|c*)?
It might not very obvious that the
?
quantifier can be removed. Without this quantifier, that pattern can still match the empty string by choosing 0 manyc
s in thec*
alternative. -
(?:\b)+
The
+
quantifier can be removed because its quantified element doesn't consume characters.
/* eslint regexp/no-useless-quantifier: "error" */
/* ✓ GOOD */
var foo = /a*/;
var foo = /(?:a|b?)??/;
var foo = /(?:\b|(?!a))*/;
/* ✗ BAD */
var foo = /a{1}/;
var foo = /(?:\b)+/;
var foo = /(?:a+b*|c*)?/;
Nothing.
This rule was introduced in eslint-plugin-regexp v0.10.0