Skip to content

regexUnnecessaryCharacterRanges

Reports character class ranges that span only one or two characters.

✅ This rule is included in the ts logical and logicalStrict presets.

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].

A range where the start and end are the same character.

const pattern = /[a-a]/;

A range that spans only two consecutive characters.

const pattern = /[a-b]/;

The rule also checks regex patterns in RegExp constructor calls.

const pattern = new RegExp("[0-1]");

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.