regexCharacterClassRanges
Reports character class elements that can be simplified to ranges.
✅ This rule is included in the ts stylistic presets.
Character class ranges in regular expressions are more concise than listing individual consecutive characters. When four or more consecutive characters appear in a character class, they can be combined into a range for better readability.
Examples
Section titled “Examples”const letters = /[abcd]/;const digits = /[0123]/;const uppercase = /[ABCD]/;const mixedWithRange = /[a-cd]/;const letters = /[a-d]/;const digits = /[0-3]/;const uppercase = /[A-D]/;// Three consecutive characters are allowed (below the threshold)const short = /[abc]/;// Non-consecutive characters cannot be simplifiedconst vowels = /[aeiou]/;// Existing ranges are already optimalconst allLetters = /[a-z]/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer listing individual characters for clarity (for example, when the characters represent specific values rather than a range), you might prefer to disable this rule. Some developers find explicit character lists easier to understand when the character set represents distinct concepts.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/prefer-range
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.