remove escaped character

11,961

Solution 1

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?

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.

Solution 2

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?

Share:
11,961
geo derek
Author by

geo derek

Updated on June 04, 2022

Comments

  • geo derek
    geo derek almost 2 years

    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

  • geo derek
    geo derek over 11 years
    Hello, other characters that are being replace are tabs to \t and line breaks to \n. And this is only happening in IE actually not Chrome or Firefox. I would like to try and use JSON.parse. Do I need to include a specific library to use it?
  • geo derek
    geo derek over 11 years
    I like this approach as it direct. However, it doesn't seem to work in IE version 9 at least. Open your jsFiddle in IE and see if you get the same issue as me.
  • Mike Samuel
    Mike Samuel over 11 years
    @geoderek, Whether you need a specific library depends on the version of IE. MSDN's JSON page says JSON.parse is "Supported in the following document modes: Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards." If you have the same trouble on IE 6 or IE 7 then github.com/douglascrockford/JSON-js or one of the other parsers from JSON.org should work.
  • geo derek
    geo derek over 11 years
    Ok I am trying it now. line of code is JSON.parse(strInfo) which returns the error: Invalid Character. The variable strInfo is the string of XML which contains the escaped characters. I am guessing that I am not using JSON.parse correctly.
  • Jan Sommer
    Jan Sommer over 11 years
    Seems like there can only be one document.write when using jsfiddle in ie... Fixed here: jsfiddle.net/GSUES
  • Mike Samuel
    Mike Samuel over 11 years
    @geoderek, Do you have double quotes around the string you are trying to decode?
  • geo derek
    geo derek over 11 years
    Hi Mike, as it turns out the IE debugger was placing these escape characters in the string only in the debug window. When I alerted the value it didn't contain them. Thanks for you help. For now I am on to a new problem which is dealing with JQuery and IE 9 nuances. Cheers, D