# regexp/no-lazy-ends

disallow lazy quantifiers at the end of an expression

  • ⚙️ This rule is included in "plugin:regexp/recommended".

# 📖 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.

Now loading...

# 🔧 Options

{
  "regexp/no-lazy-ends": [
    "error",
    {
      "ignorePartial": true,
    }
  ]
}
  • ignorePartial:

    Some regexes are used as fragments to build more complex regexes. Example:

    const 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.

      Now loading...
    • false: This rule checks all regular expressions, including those used as fragments.

      Now loading...

# ❤️ Compatibility

This rule was taken from eslint-plugin-clean-regex (opens new window).
This rule is compatible with clean-regex/no-lazy-ends (opens new window) rule.

# 🚀 Version

This rule was introduced in eslint-plugin-regexp v0.8.0

# 🔍 Implementation

Last Updated: 6/25/2022, 12:32:38 PM