javascript 删除转义字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12679521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove escaped character
提问by geo derek
I am working with a javascript function that returns a string of XML. However, within IE I get that string of XML back with escape characters embedded in it e.g. a double quote is a \”
"
Instead of
"
Is there an easy way to remove the escaped character sequence items?
Thanks,
Derek
我正在使用一个返回 XML 字符串的 javascript 函数。然而,在 IE 中,我得到了那个嵌入了转义字符的 XML 字符串,例如双引号是一个 \" " 而不是 " 有没有一种简单的方法可以删除转义的字符序列项?
谢谢,德里克
回答by Mike Samuel
Before trying to fix this, you should investigate which other characters are being replaced. For example, when you get a single \
in other browsers do you get \\
in IE?
在尝试解决此问题之前,您应该调查哪些其他字符正在被替换。例如,当您\
在其他浏览器中获得单曲时,您是否\\
在 IE 中获得单曲?
If the standard C escapes are added, then JSON.parse
will convert sequences like \"
into "
, \\
into \
, \n
into a line-feed, etc.
如果添加了标准的 C 转义符,那么JSON.parse
会将像\"
into "
、\\
into \
、\n
转换为换行符等序列。
'foo\bar\nbaz"' === JSON.parse('"foo\\bar\nbaz\""')
JSON.parse
is supported natively on most recent browsers, and on IE specifically, back to IE 8. The relevant MSDN pagesays
JSON.parse
在最新的浏览器上本机支持,特别是在 IE 上,回到 IE 8。相关的 MSDN 页面说
Supported in the following document modes: Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Also supported in Windows Store apps. See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards.
支持以下文档模式:Internet Explorer 8 标准、Internet Explorer 9 标准、Internet Explorer 10 标准。Windows 应用商店应用也支持。请参阅版本信息。
以下文档模式不支持:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准。
回答by MikeB
A similar question: Javascript - Replacing the escape character in a string literalexplains how to replace a escape character. Maybe you could replace the escape character with empty quotes?
一个类似的问题:Javascript - 替换字符串文字中的转义字符解释了如何替换转义字符。也许您可以用空引号替换转义字符?
回答by Travesty3
Use JavaScript's replace()
method.
使用 JavaScript 的replace()
方法。
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