no-useless-escape
Disallow backslashes that do not change the meaning of a string or regular expression
warn by defaulthas suggestions
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#
example.js5 problems
const quote = "It\Unnecessary escape character: \'.'s here";
const digits = new RegExp("\Unnecessary escape character: \d.d+");
const price = `\Unnecessary escape character: \#.#${amount}`;
const email = /[\w\Unnecessary escape character: \...]+\Unnecessary escape character: \@.@example\.com/;example.jsNo problems
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. |
example.jswith
{ "allowRegexCharacters": ["-", "@"] }No problemsconst slug = /^\w+\-\w+\@/;