A character range from an uppercase to a lowercase should not be used in a regular expression

  • BAD_RANGE_IN_CHARACTER_CLASS
  • Error
  • Medium
  • No tags

This rule applies when a character range in a regular expression is from an uppercase letter to a lowercase one.

The ASCII character table has several non-alphabetical characters between the A-Z and a-z ranges. For example, a character range [A-z] is equivalent to [A-Z[\\\]^_`a-z].

Usually, the lowercase letter is a typo because those non-alphabetical characters are unlikely to be intended.
When the characters are actually intended, it is recommended to write them explicitly for code readability.

Noncompliant Code Example

View with compliant examples side by side
function isAlphaNumeric(str) {
    return /^[0-9A-z]+$/.test(str); // BAD_RANGE_IN_CHARACTER_CLASS alarm
}

Compliant Code Example

View with noncompliant examples side by side
function isAlphaNumeric(str) {
    return /^[0-9A-Za-z]+$/.test(str);
}

Version

This rule was introduced in DeepScan 1.26.0.

See

Was this documentation helpful?