regexSetOperationOptimizations
Reports set operations in regular expressions that can be simplified.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
Regular expressions with the v flag support character class set operations like intersection (&&) and subtraction (--).
This rule identifies patterns where these set operations can be simplified using De Morgan’s laws.
The rule detects four types of simplifications:
- Intersection to subtraction:
[a&&[^b]]becomes[a--b] - Subtraction to intersection:
[a--[^b]]becomes[a&&b] - Negation of disjunction:
[[^a]&&[^b]]becomes[^ab] - Negation of conjunction:
[[^a][^b]]becomes[^a&&b]
Examples
Section titled “Examples”const pattern = /[a&&[^b]]/v;const pattern = /[a--[^b]]/v;const pattern = /[[^a]&&[^b]]/v;const pattern = /[[^a][^b]]/v;const pattern = /[^\S\P{ASCII}]/v;const pattern = /[a--b]/v;const pattern = /[a&&b]/v;const pattern = /[^ab]/v;const pattern = /[^a&&b]/v;const pattern = /[\s&&\p{ASCII}]/v;const pattern = /[a&&b]/v;const pattern = /[a--b]/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 form of set operations with negated operands, or if your codebase needs to support older JavaScript engines that don’t fully support the v flag, you may want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/simplify-set-operations
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.