簡體   English   中英

JavaScript 模板文字(模板字符串)中的轉義美元符號

[英]Escape dollar sign in JavaScript template literals (template strings)

我正在使用 JavaScript ES6 Docs Here的新模板文字(模板字符串)語法,我不太確定如何轉義用於破壞字符串以添加參數的美元符號。

這是我正在嘗試做的事情:

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}`;
<v-btn :href="mail">
   Send Mail
</v-btn>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM