regexCharacterClassSetOperations
Reports lookarounds that can be replaced with character class set operations.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
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.
Examples
Section titled “Examples”// Negative lookahead can be subtractionconst pattern = /(?!\d)\w/v;// Positive lookbehind can be intersectionconst pattern = /\w(?<=\d)/v;// Positive lookahead can be intersectionconst pattern = /(?=\d)\w/v;// Use subtraction instead of negative lookaheadconst pattern = /[\w--\d]/v;// Use intersection instead of positive lookbehindconst pattern = /[\w&&\d]/v;// Use intersection instead of positive lookaheadconst pattern = /[\w&&\d]/v;// Lookarounds without the v flag are validconst pattern = /(?!\d)\w/;// Complex lookarounds cannot be convertedconst pattern = /(?!abc)\w/v;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer the explicit semantics of lookarounds over set operations, this rule might not be for you.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/prefer-set-operation
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.