# regexp/no-optional-assertion

disallow optional assertions

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

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


# ❤️ Compatibility

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

# 🚀 Version

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

# 🔍 Implementation

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