org.sonar.l10n.javascript.rules.javascript.S5869.html Maven / Gradle / Ivy
Why is this an issue?
Character classes in regular expressions are a convenient way to match one of several possible characters by listing the allowed characters or
ranges of characters. If the same character is listed twice in the same character class or if the character class contains overlapping ranges, this
has no effect.
Thus duplicate characters in a character class are either a simple oversight or a sign that a range in the character class matches more than is
intended or that the author misunderstood how character classes work and wanted to match more than one character. A common example of the latter
mistake is trying to use a range like [0-99]
to match numbers of up to two digits, when in fact it is equivalent to [0-9]
.
Another common cause is forgetting to escape the -
character, creating an unintended range that overlaps with other characters in the
character class.
Character ranges can also create duplicates when used with character class escapes. These are a type of escape sequence used in regular expressions
to represent a specific set of characters. They are denoted by a backslash followed by a specific letter, such as \d
for digits,
\w
for word characters, or \s
for whitespace characters. For example, the character class escape \d
is
equivalent to the character range [0-9]
, and the escape \w
is equivalent to [a-zA-Z0-9_]
.
How to fix it
Remove the extra character, character range, or character class escape.
Code examples
Noncompliant code example
/[0-99]/ // Noncompliant, this won't actually match strings with two digits
/[0-9.-_]/ // Noncompliant, .-_ is a range that already contains 0-9 (as well as various other characters such as capital letters)
/[a-z0-9\d]/ // Noncompliant, \d matches a digit and is equivalent to [0-9]
Compliant solution
/[0-9]{1,2}/
/[0-9.\-_]/
/[a-z\d]/
Resources
Documentation
- MDN web docs - Character
classes
- MDN web docs - Character
class escape