Skip to content

regexLookaroundQuantifierOptimizations

Reports non-constant quantifiers in lookaround assertions that could be simplified.

✅ This rule is included in the ts logical presets.

Reports non-constant quantifiers in lookaround assertions that could be simplified. Lookahead and lookbehind assertions only check whether a pattern matches, not how much text it consumes. Non-constant quantifiers at the end of lookaheads or start of lookbehinds can be replaced with their minimum.

const pattern = /(?=ba*)/;
const pattern = /(?=ba+)/;
const pattern = /(?=a{4,9})/;

Lookbehinds match from right to left, so non-constant quantifiers at the start are problematic.

const pattern = /(?<=[a-c]*)/;

This rule is not configurable.

If you are intentionally using non-constant quantifiers in lookarounds for documentation purposes or if you are building patterns dynamically, you might want to disable this rule.

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