最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove escaped character - Stack Overflow

programmeradmin4浏览0评论

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

Share Improve this question asked Oct 1, 2012 at 19:13 geo derekgeo derek 3952 gold badges5 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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
发布评论

评论列表(0)

  1. 暂无评论