regexUnnecessaryDisjunctions
Reports single-character alternatives in string disjunctions.
✅ This rule is included in the ts logical presets.
Reports single-character alternatives inside \q{...} string disjunctions in regular expressions using the v flag.
When a string disjunction alternative contains only one character, that character can be placed directly in the surrounding character class instead.
Examples
Section titled “Examples”Single-Character Alternative Only
Section titled “Single-Character Alternative Only”A string disjunction containing only a single-character alternative is unnecessary.
const pattern = /[\q{a}]/v;const pattern = /[a]/v;Mixed-Length Alternatives
Section titled “Mixed-Length Alternatives”When a string disjunction has both single-character and multi-character alternatives, the single-character ones can be extracted.
const pattern = /[\q{a|bc}]/v;const pattern = /[a\q{bc}]/v;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("[\\q{a}]", "v");const pattern = new RegExp("[a]", "v");Multi-Character Alternatives Are Valid
Section titled “Multi-Character Alternatives Are Valid”String disjunctions with only multi-character alternatives are valid.
const pattern = /[\q{ab|cd}]/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 to keep single-character alternatives inside \q{...} for consistency or readability, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-useless-string-literal