-
The backslashes in the following example show as error in ESLint with "no-useless-escape". const code =
`(() => {
const regex = /\/\d+/g;
if (regex.test(window.location.href)) {
// do something
}
})();`;
browser.tabs.executeScript({code}); |
Beta Was this translation helpful? Give feedback.
Answered by
fasttime
Nov 10, 2023
Replies: 1 comment 1 reply
-
Thanks for the clarifications. Backslashes in template literals are considered escape characters, so the rule is correct in reporting them. They should be preceded by another backslash in order to result as a single backslash in the value that is assigned to const code =
`(() => {
const regex = /\\/\\d+/g;
if (regex.test(window.location.href)) {
// do something
}
})();`; Another option is using const code =
String.raw`(() => {
const regex = /\/\d+/g;
if (regex.test(window.location.href)) {
// do something
}
})();`; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
erosman
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the clarifications. Backslashes in template literals are considered escape characters, so the rule is correct in reporting them. They should be preceded by another backslash in order to result as a single backslash in the value that is assigned to
code
:Another option is using
String.raw
to get the unescaped text inside the backticks: