False positive for typescript:S5869 (duplicate regex chars) with special character classes

When using multiple special character classes in the regex, SonarCloud rule typescript:S5869 reports them as duplicated. Special characters like \n, \r, \s, \t.

const result = /^[\r\n\s\t]+/gm.exec(source);


1 Like

Hi @jerone,

I think this is being raised because the \s character class actually includes the \r, \n, and \t characters.

You should be able to write:

const result = /^\s+/gm.exec(source);

You can read more about it in MDN:

Unless I’m missing something!


Edit: I was struggling to find the right name for these, the official terminology seems to be character class escape sequences

Oh wow, how did I miss that. You are absolutely right.

\s is equivalent to [\f\n\r\t\v\u0020\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].

Based on that information, maybe this rule could use a more descriptive explanation for this particular situation. Every other duplication’s make sense, but \s special character is a combination of other special characters.

It’s not clear that all other special characters are duplication’s of \s.


Glad this helped.

You have a good point about improving the rule description.

I created a ticket to extend the explanation in the rule.

I think we can elaborate about \s, \w, and \d since those might trip other people too!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.