Skip to content

regexCharacterClassSetOperations

Reports lookarounds that can be replaced with character class set operations.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Regular expressions with the v flag have access to character class set operations like intersection (&&) and subtraction (--). These are more readable and performant than using lookarounds to achieve the same effect.

// Negative lookahead can be subtraction
const
const pattern: RegExp
pattern
= /(?!\d)\w/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
// Positive lookbehind can be intersection
const
const pattern: RegExp
pattern
= /\w(?<=\d)/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
// Positive lookahead can be intersection
const
const pattern: RegExp
pattern
= /(?=\d)\w/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.

This rule is not configurable.

If you prefer the explicit semantics of lookarounds over set operations, this rule might not be for you.

Made with ❤️‍🔥 around the world by the Flint team and contributors.