Skip to content

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.

const pattern = /(?:\b|a)?/;
const pattern = /(?:^|a)*/;
const pattern = /(?:(?=foo))?/;
const pattern = /(?:\b|(?=a))?/;

This rule is not configurable.

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.

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