regexConciseCharacterClassNegations
Reports negated character classes that can use shorthand escapes.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
Enforces using shorthand escape sequences like \D, \W, \S, and \P instead of negated character classes.
For example, [^\d] can be written more concisely as \D.
Examples
Section titled “Examples”Negated Digit Character Class
Section titled “Negated Digit Character Class”const pattern = /[^\d]/;const pattern = /\D/;Negated Word Character Class
Section titled “Negated Word Character Class”const pattern = /[^\w]+/;const pattern = /\W+/;Negated Whitespace Character Class
Section titled “Negated Whitespace Character Class”const pattern = /[^\s]/;const pattern = /\S/;Negated Unicode Property Escape
Section titled “Negated Unicode Property Escape”const pattern = /[^\p{Letter}]/u;const pattern = /\P{Letter}/u;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer the explicit negation syntax [^\d] for readability, or if your codebase has an established convention using negated character classes, 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/negation
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.