pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/simplify-set-operations |
require simplify set operations |
v2.0.0-next.11 |
💼 This rule is enabled in the ✅ plugin:regexp/recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
require simplify set operations
This rule aims to optimize patterns by simplifying set operations in character classes (with v
flag).
This rule does not report simple nested negations. (e.g. /[^[^abc]]/v
)
If you want to report simple nested negations, use the regexp/negation rule.
/* eslint regexp/simplify-set-operations: "error" */
/* ✗ BAD */
var re = /[a&&[^b]]/v; // -> /[a--b]/v
var re = /[[^b]&&a]/v; // -> /[a--b]/v
var re = /[a--[^b]]/v; // -> /[a&&b]/v
var re = /[[^a]&&[^b]]/v; // -> /[^ab]/v
var re = /[[^a][^b]]/v; // -> /[^a&&b]/v
/* ✓ GOOD */
var re = /[a--b]/v;
var re = /[a&&b]/v;
var re = /[^ab]/v;
var re = /[^a&&b]/v;
This rule attempts to simplify set operations in the ways listed below:
This rule uses De Morgan's laws to look for patterns that can convert multiple negations into a single negation, reports on them, and auto-fix them.
For example, /[[^a]&&[^b]]/v
is equivalent to /[^ab]/v
, and /[[^a][^b]]/v
is equivalent to /[^a&&b]/v
.
See https://en.wikipedia.org/wiki/De_Morgan's_laws.
Intersection sets with complement operands can be converted to difference sets.
The rule looks for character class intersection with negation operands, reports on them, auto-fix them.
For example, /[a&&[^b]]/v
is equivalent to /[a--b]/v
, /[[^a]&&b]/v
is equivalent to /[b--a]/v
.
Difference set with a complement operand on the right side can be converted to intersection sets.
The rule looks for character class subtraction with negation operand on the right side, reports on them, auto-fix them.
For example, /[a--[^b]]/v
is equivalent to /[a&&b]/v
.
This rule's auto-fix does not remove unnecessary brackets. For example, /[[^a]&&[^b]]/v
will be automatically fixed to /[^[a][b]]/v
.
If you want to remove unnecessary brackets (e.g. auto-fixed to /[^ab]/v
), use regexp/no-useless-character-class rule together.
Nothing.
This rule was introduced in eslint-plugin-regexp v2.0.0-next.11