FP S6324: Regular expressions should not contain control characters

Checking the following code snippet:

public function isTopDomain(part: string): boolean {
    return /^([^\x00-\x7F]|[a-z])+$/.test(part); // FP, the (negative) range matches all ASCII characters
}

reported by SonarQube IDE for VS Code v4.15.0

Rule S6324 states that use of control characters in a regex is most likely a mistake, which is likely true. In this case, however, I think the use of [^\x00-\x7F], is the most common (if not the only) way to describe any non-ASCII character and [\x00-\x7F] for any ASCII character.

I’m not sure if I think the rule should ignore all ranges including control characters, or this particular one, or (if there is an alternative way to describe either of the groups) describe what to do in this case.

Hello @Corniel,

thanks for reporting this. Indeed this rule is more thought towards “standard” regex, where control characters are hardly used. In your case, if you want to search specifically for them, I would say that the best is just to accept the issue.

In any case, after reading again about control characters it seems we are missing to detect x7F. I created a ticket to improve the rule.

Cheers,
Victor

1 Like