JavaScript中总会遇到字符转义的问题,但是怎么才能做到呢?
你可以在 JavaScript 中使用反斜杠来向文本字符串添加特殊字符。
反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。
var txt="We are the so-called \"Vikings\" from the north."
// We are the so-called "Vikings" from the north.
下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:
代码 | 输出 |
---|---|
\' | 单引号 |
\" | 双引号 |
\& | 和号 |
\\ | 反斜杠 |
\n | 换行符 |
\r | 回车符 |
\t | 制表符 |
\b | 退格符 |
\f | 换页符 |