regexDigitMatchers
Reports character classes with multiple adjacent characters that could use a range instead.
✅ This rule is included in the ts stylisticStrict presets.
Enforces using \d instead of [0-9] and \D instead of [^0-9] in regular expressions.
The escape sequences are more concise and widely recognized.
Examples
Section titled “Examples”Digit Character Class
Section titled “Digit Character Class”const pattern = /[0-9]+/;const pattern = /\d+/;Negated Digit Character Class
Section titled “Negated Digit Character Class”const pattern = /[^0-9]/;const pattern = /\D/;All Digits Listed
Section titled “All Digits Listed”const pattern = /[0123456789]/;const pattern = /\d/;RegExp Constructor
Section titled “RegExp Constructor”const pattern = new RegExp("[0-9]+");const pattern = new RegExp("\\d+");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase has an established convention of using [0-9] for readability, or if you need consistency with patterns that mix digit ranges with other characters (like [0-9a-f] for hexadecimal), 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/prefer-d
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.