Skip to content

regexSetOperationOptimizations

Reports set operations in regular expressions that can be simplified.

✅ This rule is included in the ts logical and logicalStrict presets.

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:

  1. Intersection to subtraction: [a&&[^b]] becomes [a--b]
  2. Subtraction to intersection: [a--[^b]] becomes [a&&b]
  3. Negation of disjunction: [[^a]&&[^b]] becomes [^ab]
  4. Negation of conjunction: [[^a][^b]] becomes [^a&&b]
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;

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.