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 const pattern: RegExp
pattern = /a\bb/;const const pattern2: RegExp
pattern2 = /-\b-/;const const pattern: RegExp
pattern = /a\b-/;const const pattern2: RegExp
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 const pattern: RegExp
pattern = /a\B-/;const const pattern: RegExp
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 const pattern: RegExp
pattern = /a^b/;const const pattern2: RegExp
pattern2 = /a$b/;const const pattern: RegExp
pattern = /^ab/;const const pattern2: RegExp
pattern2 = /ab$/;const const pattern3: RegExp
pattern3 = /a^b/m;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("a\\bb");const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
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 ❤️🔥 around the world by
the Flint team and contributors.