问题描述
我如何在 ECMAScript 6 中编写一个模板文字,其中包含反引号(`),并单独包含反引号(即嵌套反引号)?
How can I write a template literal in ECMAScript 6 that will contain backticks(`) in and by itself, (i.e. nested backticks)?
例如:
var query = `
UPDATE packet
SET
`association` = "3485435",
`tagname` = "associated"
`
我需要它的原因:
The reason I need it:
在我上面的代码示例中很明显.
It's quite obvious in my code example above.
我正在尝试将 node-mysql 查询构建为 Strings
并将它们存储在一个变量中,以便将它们传递给 MySQL.MySQL 查询语法需要对 UPDATE
样式的查询进行反勾.
I'm trying to build node-mysql queries as Strings
and store them in a variable for passing them to MySQL. The MySQL query syntax requires back ticks for UPDATE
-style queries.
我能让它们看起来整洁的唯一方法整洁是通过使用模板文字,否则使用常规单行字符串的查询看起来很糟糕,因为在某些情况下它们最终会很长.
The only way I can have them look neat & tidy is by using template literals, otherwise the queries using regular single-line strings look awful because they end up being very long is some cases.
我还想避免使用 终止行,因为它很麻烦.
I also want to avoid terminating lines using as it's cumbersome.
推荐答案
来自 深入 ES6:Jason Orendorff 的模板字符串:
如果你需要在模板字符串中写一个反引号,你必须用反斜杠对其进行转义:```
与 "`"
相同.
您的查询应该是:
var query = `UPDATE packet
SET
`association` = "3485435",
`tagname` = "Simos"`
这篇关于ES6 中带有嵌套反引号 (`) 的模板文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!