JavaScript模板文字(模板字符串)中的转义美元符号 [英] Escape dollar sign in JavaScript template literals (template strings)

查看:734
本文介绍了JavaScript模板文字(模板字符串)中的转义美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript ES6的新模板文字(模板字符串)语法这里的文档,我不太确定如何转义用于断开字符串以添加参数的美元符号。

I am using the new template literals (template strings) syntax of JavaScript ES6 Docs Here and I am not quite sure how to escape the dollar sign that is used to break the string to add a parameter.

这就是我的意思尝试做:

Here is what I am trying to do:

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}

that works fine... but I would really prefer to not have that space $ ${title}

这样的空间,使最终结果看起来像这样:

that leaves the end result looking like :


...购买金额为$ 25.99 ...

... in the amount of $ 25.99 for the purchase...

我真的更希望


...购买金额为$ 25.99 ...

... in the amount of $25.99 for the purchase ...

我想这是可以的,或者显然我可以使用仍然有效的旧方法,但是知道如何解决此问题将非常不错。我链接到Mozilla文档,但在其中找不到任何内容,希望有人对如何解决这个问题有所了解

I guess that is ok, or obviously I could use the old way that still works, but it would be nice to know how to fix this. I linked to the Mozilla docs , and I can't find anything in there about it, hopefully someone has an idea how to fix this

推荐答案

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.`

这篇关于JavaScript模板文字(模板字符串)中的转义美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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