Skip to content

regexUnnecessarySetOperands

Reports unnecessary operands in regular expression character class set operations.

✅ This rule is included in the ts logical presets.

Regular expressions with the v flag support character class set operations like intersection (&&) and subtraction (--). This rule identifies operands in these operations that are unnecessary or produce unexpected results.

The rule detects four types of issues:

  1. Disjoint intersection: two sets with no common elements intersected together, producing an empty result.
  2. Subset intersection: a superset operand in an intersection that is redundant.
  3. Disjoint subtraction: subtracting a set that shares no elements, which has no effect.
  4. Subset subtraction: subtracting a superset, which produces an empty result.
/[\w&&\s]/v;
/[\w&&\d]/v;
/[\w--\s]/v;
/[\d--\w]/v;
/[[abc]&&[def]]/v;

This rule is not configurable.

If you intentionally use set operations that produce empty results for documentation or testing purposes, you may want to disable this rule.

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