6

I am working on a system that generates JavaScript, and I would like to minimize the amount of escaping that needs to be done for string literals. My current approach is to use the String.raw template literal function so that I don't have to worry characters like \ being interpreted in the output.

The only issue is that literal backticks (i.e. ` characters) still need to be escaped, because otherwise they would terminate the string early. They can't be escaped as \` because escape sequences are not honored.

The best alternative I can think of is to use a placeholder (e.g. String.raw`foo${"`"}bar` to get "foo`bar"), but I am somewhat concerned about the runtime cost of the evaluation of placeholders. Are there any other alternatives?

Mihai Parparita
  • 4,236
  • 1
  • 23
  • 30

0 Answers0