regexUnnecessaryOptionalAssertions
Reports assertions inside optional quantifiers that have no effect.
✅ This rule is included in the ts logical presets.
Assertions (like \b, ^, $, (?=...), (?<=...)) that are inside optional quantifiers (?, *, {0,n}) where all paths to the assertion consume no characters.
These assertions are useless because the regex engine can skip the entire quantified element without any impact.
This rule reports on any assertion that are unnecessary for being inside optional quantifiers.
Examples
Section titled “Examples”const pattern = /(?:\b|a)?/;const pattern = /(?:^|a)*/;const pattern = /(?:(?=foo))?/;const pattern = /(?:\b|(?=a))?/;const pattern = /(?:\b-)?/;const pattern = /(?:^a)*/;const pattern = /(?=foo)/;const pattern = /fo(?:o\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 constructing patterns dynamically or using patterns as partials that will be combined with other patterns, you might need to disable this rule. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-optional-assertion