Skip to content

no-useless-escape issue in regexp strings #17744

Answered by fasttime
erosman asked this question in Q&A
Discussion options

You must be logged in to vote

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:

const code = 
`(() => {
  const regex = /\\/\\d+/g;
  if (regex.test(window.location.href)) {
    // do something
  }
})();`;

Another option is using String.raw to get the unescaped text inside the backticks:

const code = 
String.raw`(() => {
  const regex = /\/\d+/g;
  if (regex.test(window.location.href)) {
    // do something
  }
})();`;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@erosman
Comment options

Answer selected by erosman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants