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.
Examples
Section titled “Examples”Lookahead with Zero Minimum
Section titled “Lookahead with Zero Minimum”const pattern = /(?=ba*)/;const pattern = /(?=b)/;Lookahead with One Minimum
Section titled “Lookahead with One Minimum”const pattern = /(?=ba+)/;const pattern = /(?=ba)/;Lookahead with Range Minimum
Section titled “Lookahead with Range Minimum”const pattern = /(?=a{4,9})/;const pattern = /(?=a{4})/;Lookbehind Assertions
Section titled “Lookbehind Assertions”Lookbehinds match from right to left, so non-constant quantifiers at the start are problematic.
const pattern = /(?<=[a-c]*)/;const pattern = /(?<=)/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.