regexp/no-lazy-ends
⚠️ This rule warns in the following configs: 🟢 flat/recommended
, 🔵 recommended
.
💡 This rule is manually fixable by editor suggestions.
disallow lazy quantifiers at the end of an expression
📖 Rule Details
If a lazily quantified element is the last element matched by an expression (e.g. the a{2,3}?
in b+a{2,3}?
), we know that the lazy quantifier will always only match the element the minimum number of times. The maximum is completely ignored because the expression can accept after the minimum was reached.
If the minimum of the lazy quantifier is 0, we can even remove the quantifier and the quantified element without changing the meaning of the pattern. E.g. a+b*?
and a+
behave the same.
If the minimum is 1, we can remove the quantifier. E.g. a+b+?
and a+b
behave the same.
If the minimum is greater than 1, we can replace the quantifier with a constant, greedy quantifier. E.g. a+b{2,4}?
and a+b{2}
behave the same.
🔧 Options
{
"regexp/no-lazy-ends": [
"error",
{
"ignorePartial": true,
}
]
}
ignorePartial
:Some regexes are used as fragments to build more complex regexes. Example:
jsconst any = /[\s\S]*?/.source; const pattern = RegExp(`<script(\\s${any})?>(${any})<\\/script>`, "g");
In these fragments, seemingly ignored quantifier might not actually be ignored depending on how the fragment is used.
true
: The rule does not check the regexp used as a fragment. This is default.false
: This rule checks all regular expressions, including those used as fragments.
❤️ Compatibility
This rule was taken from eslint-plugin-clean-regex.
This rule is compatible with clean-regex/no-lazy-ends rule.
🚀 Version
This rule was introduced in eslint-plugin-regexp v0.8.0