问题描述
<html>
<body>
<p id="demo"></p>
<script>
var x = 'It\'s \@lright';
var y = "We are the so-called \"Vikings\" from the north.";
var i;
var s = "";
var z = "Thi$ is * a@ demo";
var patt = "[^A-Za-z0-9\\s]";
for(i=0;i<z.length;i++){
if(z[i].match(patt)){
s+= "\\"+z[i];
}
else{
s+= z[i];
}
}
document.getElementById("demo").innerHTML = x + "<br>" + y + "<br>" + s;
</script>
</body>
</html>
我得到的上述脚本的输出是:
The output for the above script I get is :
我不知道为什么对于字符串x
和y
,如果添加任何特殊字符后跟'\'
(反斜杠),则字符串将按原样显示.但是,当我通过在每个特殊字符之前添加'\'
来对字符串s执行操作时,该字符串将在特殊字符之前显示'\'
.
I don't know why for the strings x
and y
, if I add any special character followed by '\'
(backslash), strings are displayed as it is with the special characters. But when I do it to string s by appending '\'
before each special character, the string gets displayed with '\'
preceding the special characters.
有人可以向我解释一下吗?并且还请告诉我如何在每个特殊字符之前添加"\",这样当我打印字符串时,我只会看到特殊字符(不是现在出现在特殊字符前面的'\'
)?
Can someone explain me this? And also please tell me how to add '\' before each special character in such a way that when I print the string I only see the special characters (not with '\'
prepended in front of them as happening now)?
推荐答案
反斜杠字符表示字符串转义字符.当在字符串中找到该字符时,JavaScript运行时将知道跟随该字符的字符将指示要转义的实际字符.如果只想包含一个反斜杠,则必须为此提供转义代码,即两个反斜杠.
The backslash character is the indicator of a string escape character. When it is found in a string, the JavaScript runtime knows that the character that will follow it dictates what actual character to escape. If you just want to include a single backslash, then you'll have to provide the escape code for that, which is two backslashes.
console.log("\"This string has double quotes embedded within double quotes\"");
// If we want to output a single backslash, we need to escape with with \\
// So, if we want to produce two of them, we need \\\\
console.log("The escape code for a \\ is \\\\");
console.log("The escape code for a \' is \\'");
console.log("The escape code for a \" is \\\"");
现在,重要的是用HTML实体代码区分"JavaScript"字符串转义序列.
Now, it's important to distinguish a "JavaScript" string escape sequence with an HTML entity code.
如果您采用包含JavaScript转义字符的JavaScript字符串并将该字符串发送给HTML解析器进行解析,则转义序列将已经被处理,HTML解析器将接收该工作的结果. HTML解析器将仅呈现正常传递的实际字符.
If you take a JavaScript string that includes JavaScript escape characters and send that string to be parsed by the HTML parser, the escape sequences will have already been processed and the HTML parser will be receiving the result of that work. The HTML parser will just render the actual characters that were passed as it normally would.
JavaScript转义字符仅在由JavaScript运行时处理时才有意义,而不是HTML解析器:
The JavaScript escape characters only have meaning when processed by the JavaScript runtime, not the HTML parser:
var x = "This will be split over two lines when processed by the JavaScript runtime, \n\nbut the escape code will will just cause normal carriage return white space for the HTML parser";
var y = "\"To escape or not to escape\"";
// Escape code will be parsed and result in two groups of text
alert(x);
// Here, the result of the escaping will be passed to the HTML parser for further processing
// But here, the new line characters embedded within the string will be treated as all exteraneous
// white space in an HTML document is - - it will be normalized down to a single space.
document.querySelector("#d1").innerHTML = x;
document.querySelector("#d2").innerHTML = y;
<div id="d1"></div>
<div id="d2"></div>
这篇关于将转义字符添加到javascript中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!