删除转义字符

时间:2012-10-01 19:13:57

标签: javascript escaping

我正在使用一个返回XML字符串的javascript函数。但是,在IE中,我得到了一串XML,其中嵌入了转义字符,例如双引号是“ “ 代替 “ 是否有一种简单的方法可以删除转义的字符序列项? 谢谢, 德里克

3 个答案:

答案 0 :(得分:2)

在尝试解决此问题之前,您应该调查哪些其他字符正在被替换。例如,当您在其他浏览器中获得单个\时,您会在IE中获得\\吗?

如果添加了标准C转义符,则JSON.parse会将\"等序列转换为"\\转换为\\n进入换行等等。

'foo\\bar\nbaz"' === JSON.parse('"foo\\\\bar\\nbaz\\""')

JSON.parse在最近的浏览器上本机支持,特别是在IE上,回到IE 8. relevant MSDN page

  

在以下文档模式中受支持: Internet Explorer 8标准,Internet Explorer 9标准,Internet Explorer 10标准。 Windows应用商店应用也支持。请参阅版本信息。

     

以下文档模式不支持:Quirks,Internet Explorer 6标准,Internet Explorer 7标准。

答案 1 :(得分:1)

一个类似的问题:Javascript - Replacing the escape character in a string literal解释了如何替换转义字符。也许你可以用空引号替换转义字符?

答案 2 :(得分:0)

使用JavaScript的replace()方法。

jsFiddle

var string1 = "This is a string with all the \\\" characters escaped";

document.write(string1);    // outputs: This is a string with all the \" characters escaped

document.write("<br />");

string1 = string1.replace("\\", "");

document.write(string1);    // outputs: This is a string with all the " characters escaped