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;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
/[\w&&\d]/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
/[\w--\s]/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
/[\d--\w]/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.
/[[abc]&&[def]]/v;
Error ts(1501) ― This regular expression flag is only available when targeting 'es2024' or later.

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 ❤️‍🔥 around the world by the Flint team and contributors.