Home > Web Front-end > JS Tutorial > JavaScript Special Characters_Basic Knowledge

JavaScript Special Characters_Basic Knowledge

WBOY
Release: 2016-05-16 19:15:35
Original
1086 people have browsed it

You can use backslashes in JavaScript to add special characters to text strings.

Insert special characters

Backslashes are used to insert ellipses, newlines, quotes, and other special characters in text strings.

Please see the JavaScript code below:

var txt="We are the so-called <code>"Vikings"</code> from the north."
document.write(txt)
Copy after login

In JavaScript, strings use single quotes or double quotes to start or end. This means that the above string will be truncated to: We are the so-called.

To solve this problem, you must add a backslash () before the quotation mark in "Viking". This converts each double quote into a literal string.

var txt="We are the so-called <code>\"Vikings\"</code> from the north."
document.write(txt)
Copy after login

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.

Here’s another example:

document.write ("You <code>\&</code> me are singing!") 
Copy after login

The above example produces the following output:

You & me are singing!
Copy after login

The following table lists the remaining special characters that can be added to text strings using backslashes:

代码 输出
' 单引号
" 双引号
& 和号
\ 反斜杠
n 换行符
r 回车符
t 制表符
b 退格符
f 换页符
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template