Including a literal backtick in a String.raw template literal
Asked Answered
W

0

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?

Welty answered 26/8, 2019 at 4:39 Comment(1)
The only alternative i can think of is string concatenation, but to me the one you specified above seems fineGlasswork

© 2022 - 2024 — McMap. All rights reserved.