Skip to content

regexp/no-empty-lookarounds-assertion

💼 This rule is enabled in the following configs: 🟢 flat/recommended, 🔵 recommended.

disallow empty lookahead assertion or empty lookbehind assertion

📖 Rule Details

This rule reports empty lookahead assertion or empty lookbehind assertion.

What are empty lookarounds?

An empty lookaround is a lookaround for which at least one path in the lookaround expression contains only elements that do not consume characters and do not assert characters. This means that the lookaround expression will trivially accept any input string.

Examples:

  • (?=): One of the simplest empty lookarounds.
  • (?=a*): It is possible for a* to not consume characters, therefore the lookahead is empty.
  • (?=a|b*): Only one path has to not consume characters. Since it is possible for b* to not consume characters, the lookahead is empty.
  • (?=a|$): This is not an empty lookaround. $ does not consume characters but it does assert characters. Similarly, all other standard assertions (\b, \B, ^) are also not empty.

Why are empty lookarounds a problem?

Because empty lookarounds accept the empty string, they are essentially non-functional. They will always trivially reject/accept.

E.g. (?=b?)\w will match a just fine. (?=b?) will always trivially accept no matter the input string. The same also happens for negated lookarounds but they will trivially reject. E.g. (?!b?)\w won't match any input strings.

The only way to fix empty lookarounds is to either remove them or to rewrite the lookaround expression to be non-empty.

Now loading...

🔧 Options

Nothing.

🚀 Version

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

🔍 Implementation