转义字符。将有特殊意义的符号转换为其本身。 比如,JS中表示正则表达式的符号是/ var a = /your string/;但是,如果你想匹配 / 这个字符本身呢? 比如匹配 string/ 很明显直接写 var a = /string//; 这样会造成语法错误这时就需要进行字符转义了 var a = /string\//; 这样就可以匹配正确的结果了需要转义的特殊字符有: / \ [ ] ( ) ? * + 等等
反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。请看下面的 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 换页符
比如,JS中表示正则表达式的符号是/
var a = /your string/;但是,如果你想匹配 / 这个字符本身呢?
比如匹配 string/
很明显直接写
var a = /string//;
这样会造成语法错误这时就需要进行字符转义了
var a = /string\//;
这样就可以匹配正确的结果了需要转义的特殊字符有: / \ [ ] ( ) ? * + 等等
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 换页符