fai*_*aid 0 javascript backslash
JavaScript中的"\"字符有什么问题?
此脚本不起作用:
var theText='<he:/ll/*o?|>'
function clean(txt) {
var chr = [ '\', '/', ':', '*', '?', '<', '>', '|' ];
for(i=0;i<=8;i++){txt=txt.split(chr[i]).join("")}
return txt;}
alert(clean(theText));
Run Code Online (Sandbox Code Playgroud)
当我从数组中删除"反斜杠"时它可以工作:
var theText='<he:/ll/*o?|>'
function clean(txt) {
var chr = [ '/', ':', '*', '?', '<', '>', '|' ];
for(i=0;i<=7;i++){txt=txt.split(chr[i]).join("")}
return txt;}
alert(clean(theText));
Run Code Online (Sandbox Code Playgroud)
我写的时候它不起作用 var txt='text\';
这个错误可能来自加入反斜杠的引号,如下所示:\'或'\'
但我也需要/字符,我该怎么办?
反斜杠逃避了收盘价.你需要逃避反斜杠本身:
var chr = [ '\\', '/', ':', '*', '?', '<', '>', '|' ];
// ^--- Add another backslash to escape the original one
Run Code Online (Sandbox Code Playgroud)
例如,如果您想在数组中添加单引号字符,则此行为可能会有用:
var chr = [ ''', '/', ':', '*', '?', '<', '>', '|' ];
// ^--- This quote closes the first and the 3rd will cause an error
Run Code Online (Sandbox Code Playgroud)
通过转义单引号,它被视为"正常"字符,不会关闭字符串:
var chr = [ '\'', '/', ':', '*', '?', '<', '>', '|' ];
// ^--- Escaped quote, no problem
Run Code Online (Sandbox Code Playgroud)
您应该能够从Stack Overflow应用的语法突出显示中看到前两个示例之间的差异.
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |