# regexp/optimal-lookaround-quantifier

disallow the alternatives of lookarounds that end with a non-constant quantifier

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

# 📖 Rule Details

Non-constant quantifiers are quantifiers that describe a range (e.g. ?, *, +, {0,1}, {5,9}, {3,}). They have to match some number of times (the minimum) after which further matches are optional until a certain maximum (may be infinite) is reached.

It's obvious that /ba{2}/ and /ba{2,6}/ will match differently because of the different quantifiers of a but that not the case if for lookarounds. Both /b(?=a{2})/ and /b(?=a{2,6})/ will match strings the same way. I.e. for the input string "baaa", both will create the same match arrays. The two regular expression are actually equivalent, meaning that (?=a{2}) is equivalent to (?=a{2,6}).

More generally, if a non-constant quantifier is an end of the expression tree of a lookahead, that quantifier can be replaced with a constant quantifier that matched the element minimum-if-the-non-constant-quantifier many times. For lookbehinds, the non-constant quantifier has to be at the start of the expression tree as lookbehinds are matched from right to left.

Now loading...

# 🔧 Options


# ❤️ Compatibility

This rule was taken from eslint-plugin-clean-regex (opens new window).
This rule is compatible with clean-regex/optimal-lookaround-quantifier (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