Skip to content

regexp/no-optional-assertion

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

disallow optional assertions

📖 Rule Details

Assertions that are quantified (directly or indirectly) can be considered optional if the quantifier has a minimum of zero.

A simple example is the following pattern: /a(?:$)*b/. The $ assertion will reject, but if that happens it will simply be ignored because of the * quantifier. The assertion is optional, serving no function whatsoever.

More generally, an assertion is optional, if there exists a parent quantifier with a minimum of zero such that all possible paths of the quantified element that contain the assertion do not consume characters.

Here's an example: a(?:foo|(?<!-)(?:-|\b))*b. The \b is optional. However, the lookbehind (?<!-) is not optional because the group (?:-|\b) right after it can consume a character.

Optional assertions don't affect the pattern in any way. They are essentially dead code.

Now loading...

🔧 Options

Nothing.

❤️ Compatibility

This rule was taken from eslint-plugin-clean-regex.
This rule is compatible with clean-regex/no-optional-assertion rule.

🚀 Version

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

🔍 Implementation