Linter · Rules
no-useless-escape
Disallow backslashes that do not change the meaning of a string or regular expression
In "\d" the backslash is dropped and the string is "d". Either you don't need the backslash, or you wanted a real backslash and need to write "\\d".
The rule checks strings, template literals, and regular expression literals. Tagged templates are skipped, because the tag function can see the raw text.
This rule is on by default.
Configure
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"no-useless-escape" = "off" # or "error"
Every option, set to its default:
bunfig.toml
[lint.rules]
"no-useless-escape" = ["warn", {
allowRegexCharacters = [], # list of strings
}]
Examples
Reported:
const quote = "It\'s here";
const digits = new RegExp("\d+");
const price = `\#${amount}`;
const email = /[\w\.]+\@example\.com/;
Not reported:
const quote = "It's here";
const nested = "She said \"hi\"";
const digits = new RegExp("\\d+");
const price = `#${amount} \${literal}`;
const email = /[\w.]+@example\.com/;
const raw = String.raw`C:\Users\me`;
Options
| Option | Default | What it does |
|---|---|---|
allowRegexCharacters | [] | Characters that you may always escape in a regular expression literal. |
const slug = /^\w+\-\w+\@/;