org.sonar.l10n.javascript.rules.javascript.S6535.html Maven / Gradle / Ivy
Why is this an issue?
The \
(backslash) character indicates that the next character should be treated as a literal character rather than as a special
character or string delimiter. For instance, it is common to escape single quotes inside a string literal using the single quote delimiter like
'It\'s a beautiful day'
. Escaping is only meaningful for special characters. Escaping non-special characters in strings, template
literals, and regular expressions doesn’t affect their value.
const regex = /[\[]/; // Noncompliant: '[' does not need to be escaped when inside a character class '[]'
const octal = '\8'; // Noncompliant: '8' is not valid octal number
const hello = 'Hello, world\!'; // Noncompliant: '!' is not a special character
const path = `\/${some}\/${dir}`; // Noncompliant: '/' is not a special character
Therefore, useless escapes impact code readability and could even denote a bug in the code if the developer left it by mistake or intended to
escape another special character instead.
You should check if the escape character was not misplaced. Useless character escapes can safely be removed without changing the original
value.
const regex = /[[]/;
const octal = '8';
const hello = 'Hello, world!';
const path = `/${some}/${dir}`;
Resources
Documentation
- MDN web docs - Escaping in Regular
expressions
- W3Schools - JavaScript strings