ES6中带有嵌套反引号(`)的模板文字 [英] Template literals with nested backticks(`) in ES6

查看:1125
本文介绍了ES6中带有嵌套反引号(`)的模板文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ES6中编写一个模板文字,其中包含反引号(`)及其自身(即嵌套反引号)。

How can I write a template literal in ES6 that will contain backticks(`) in and by itself, (i.e nested backticks).

例如:

var query = `
  UPDATE packet
  SET
  `association` = "3485435",
  `tagname` = "associated"
 `






我需要它的原因:


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.

我也想要避免使用 \ n 终止行,因为它很麻烦。

I also want to avoid terminating lines using \n as it's cumbersome.

推荐答案

来自 ES6 In Depth:Jason Orendorff的模板字符串


如果你需要在模板字符串中写一个反引号,你必须用反斜杠转义它:`\```相同。

您的查询应该是:

var query = `UPDATE packet
  SET
  \`association\` = "3485435",
  \`tagname\` = "Simos"`

这篇关于ES6中带有嵌套反引号(`)的模板文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆