Skip to content

regexUnnecessaryAssertions

Reports assertions in regular expressions that always reject.

✅ This rule is included in the ts logical presets.

Reports assertions in regular expressions that always reject. These patterns will never match because the assertion contradicts its surrounding context.

Word Boundary Between Same Character Types

Section titled “Word Boundary Between Same Character Types”

A word boundary (\b) always rejects when placed between two word characters or two non-word characters.

const pattern = /a\bb/;
const pattern2 = /-\b-/;

A negated word boundary (\B) always rejects when there is a word/non-word transition.

const pattern = /a\B-/;

Start (^) and end ($) anchors always reject when not at pattern boundaries (without the multiline flag).

const pattern = /a^b/;
const pattern2 = /a$b/;

The rule also checks regex patterns in RegExp constructor calls.

const pattern = new RegExp("a\\bb");

This rule is not configurable.

If you are working with dynamically constructed regex patterns where the simplified detection may cause false positives, you may disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.