regexUnnecessaryCharacterRanges
Reports character class ranges that span only one or two characters.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
Reports character class ranges that are unnecessary because they span only one or two characters.
A range like [a-a] matches only one character and can be simplified to [a].
A range like [a-b] matches only two adjacent characters and can be written as [ab].
Examples
Section titled “Examples”Identity Range
Section titled “Identity Range”A range where the start and end are the same character.
const pattern = /[a-a]/;const pattern = /[a]/;Adjacent Range
Section titled “Adjacent Range”A range that spans only two consecutive characters.
const pattern = /[a-b]/;const pattern = /[ab]/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("[0-1]");const pattern = new RegExp("[01]");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 range syntax for readability or consistency, even when it spans only one or two characters, 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-range
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.