Disallow lazy quantifiers at the end of an expression.
configuration in plugin:clean-regex/recommended
: "warn"
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.
Examples of valid code for this rule:
/a+?b*/
/a??(?:ba+?|c)*/
/ba*?$/
Examples of invalid code for this rule:
/a??/
/a+b+?/
/a(?:c|ab+?)?/