You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A community member opened a thread for rule S5869 (duplicate chars in regex character class) thinking there was a false positive:
constresult=/^[\r\n\s\t]+/gm.exec(source);
However, the rule is working as expected, since \s includes \r, \n, \t, among other invisible characters.
So the regex could be written this way:
constresult=/^\s+/gm.exec(source);
This means the rule is accurate, however, it was not obvious from the rule description.
Understandably, users would expect to see an explicit duplicate character since this is the first example provided:
/[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)
Explanation
A community member opened a thread for rule S5869 (duplicate chars in regex character class) thinking there was a false positive:
However, the rule is working as expected, since
\s
includes\r
,\n
,\t
, among other invisible characters.So the regex could be written this way:
This means the rule is accurate, however, it was not obvious from the rule description.
Understandably, users would expect to see an explicit duplicate character since this is the first example provided:
Suggestion
We could improve the rule description to mention that there are special escape characters that work like character classes:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape
\d
,\D
,\s
,\S
,\w
,\W
, since some developers might not be familiar with them\s
, like in the case reported by the user in the community thread.I think this will make the rule more useful, and help with the learning aspect.
The text was updated successfully, but these errors were encountered: