我正在使用 JavaScript ES6 的新模板文字(模板字符串)语法此处文档,我不太确定如何转义用于破坏字符串以添加参数的美元符号。
这就是我正在尝试做的事情:
var response = `I consent to my credit card being charged in the amount of
$ ${ total } for the purchase of ${ item.title } and any
applicable sales tax.`
效果很好...但我真的不想有那个空间
$ ${title}
最终结果看起来像这样:
...购买金额为 25.99 美元...
我真的更喜欢
...购买金额为 25.99 美元...
我想这没问题,或者显然我可以使用仍然有效的旧方法,但很高兴知道如何解决这个问题。我链接到 Mozilla 文档,但我找不到任何相关内容,希望有人知道如何解决这个问题
$
不产生文字$
的唯一情况是在{
之前,否则你不需要转义它。
var response = `You have $${money}`
因此有效。如果您需要转义任何内容,反斜杠
\
也是模板字符串中的转义字符,因此(虽然不必要)以下内容也适用:
var response = `You have \$${money}`
var response = `I consent to my credit card being charged in the amount of
$${ total } for the purchase of ${ item.title } and any
applicable sales tax.`
在美元符号前面加一个反斜杠是我想到的第一件事,而且它有效:
\${DEPLOYMENT_NAME}
这对我有用。
var response = `You have \$\{money}`;
你可以这样做(使用 Node.js 测试)
`$\${money}`
\$
是 $
的转义字符