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
const pattern: RegExp
pattern
= /[a-a]/;

A range that spans only two consecutive characters.

const
const pattern: RegExp
pattern
= /[a-b]/;

The rule also checks regex patterns in RegExp constructor calls.

const
const pattern: RegExp
pattern
= new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
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 ❤️‍🔥 around the world by the Flint team and contributors.