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.
Examples
Section titled “Examples”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-/;const pattern = /a\b-/;const pattern2 = /-\ba/;Negated Word Boundary at Transition
Section titled “Negated Word Boundary at Transition”A negated word boundary (\B) always rejects when there is a word/non-word transition.
const pattern = /a\B-/;const pattern = /a\Ba/;Anchors Not at Pattern Boundaries
Section titled “Anchors Not at Pattern Boundaries”Start (^) and end ($) anchors always reject when not at pattern boundaries (without the multiline flag).
const pattern = /a^b/;const pattern2 = /a$b/;const pattern = /^ab/;const pattern2 = /ab$/;const pattern3 = /a^b/m;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("a\\bb");const pattern = new RegExp("a\\b-");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you are working with dynamically constructed regex patterns where the simplified detection may cause false positives, you may disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-useless-assertions
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.