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
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
3 Answers
Reset to default 2Before 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?
If the standard C escapes are added, then JSON.parse
will convert sequences like \"
into "
, \\
into \
, \n
into a line-feed, etc.
'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 page says
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.
A similar question: Javascript - Replacing the escape character in a string literal explains how to replace a escape character. Maybe you could replace the escape character with empty quotes?
Use JavaScript's replace()
method.
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