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 pattern = /(?!\d)\w/v;
// Positive lookbehind can be intersection
const pattern = /\w(?<=\d)/v;
// Positive lookahead can be intersection
const pattern = /(?=\d)\w/v;

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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.