【Javascript】javascript學習 二十 JavaScript 特殊字元

來源:互聯網
上載者:User

你可以在 JavaScript 中使用反斜線來向文本字串添加特殊字元。

插入特殊字元

反斜線用來在文本字串中插入省略符號、分行符號、引號和其他特殊字元。

請看下面的 JavaScript 代碼:

var txt="We are the so-called "Vikings" from the north."document.write(txt)

在 JavaScript 中,字串使用單引號或者雙引號來起始或者結束。這意味著上面的字串將被截為:We are the so-called。

要解決這個問題,就必須把在 "Viking" 中的引號前面加上反斜線 (\)。這樣就可以把每個雙引號轉換為字面上的字串。

var txt="We are the so-called \"Vikings\" from the north."document.write(txt)

現在 JavaScript 就可以輸出正確的文本字串了:We are the so-called "Vikings" from the north。

這是另一個例子:

document.write ("You \& me are singing!") 

上面的例子會產生以下輸出:

You & me are singing!

下面的表格列出了其餘的特殊字元,這些特殊字元都可以使用反斜線來添加到文本字串中:

代碼 輸出
\' 單引號
\" 雙引號
\& 和號
\\ 反斜線
\n 分行符號
\r 斷行符號符
\t 定位字元
\b 退格符
\f 換頁符
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.