Character classes in a regular expression should not have duplicate characters
- DUPLICATES_IN_CHARACTER_CLASS
- Code Quality
- Medium, Low
- No tags
This rule applies when a character class in a regular expression has duplicate characters.
A character class, also called character set, is specified using square brackets []
. It matches exactly one character among the characters appearing inside the square brackets.
It is recommended to remove duplicate characters in a character class because the same set of characters is specified without the duplicates.
Moreover, the duplicates may be caused by a programmer's misunderstanding of characters like |
, ,
, (
and )
, which have no special meaning inside a character class.
If you intended an exact match of duplicate characters, you need to use capturing parentheses ()
.
Noncompliant Code Example
View with compliant examples side by sidefunction isOdd(str) {
return /^\d*[1|3|5|7|9]$/.test(str); // DUPLICATES_IN_CHARACTER_CLASS alarm because '|' appears multiple times.
}
function endsWithDeepOrScan(word) {
return /[deep|scan]$/i.test(word) // DUPLICATES_IN_CHARACTER_CLASS alarm because 'e' appears multiple times.
}
Compliant Code Example
View with noncompliant examples side by sidefunction isOdd(str) {
return /^\d*[13579]$/.test(str);
}
function endsWithDeepOrScan(word) {
return /(deep|scan)$/i.test(word)
}
Version
This rule was introduced in DeepScan 1.26.0.