Skip to content

regexp/optimal-lookaround-quantifier

⚠️ This rule warns in the following configs: 🟢 flat/recommended, 🔵 recommended.

💡 This rule is manually fixable by editor suggestions.

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

📖 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 (potentially 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 expressions 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

Nothing.

❤️ Compatibility

This rule was taken from eslint-plugin-clean-regex.
This rule is compatible with clean-regex/optimal-lookaround-quantifier rule.

🚀 Version

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

🔍 Implementation