2012-10-01 54 views
2

我正在使用返回XML字符串的javascript函數。然而,在IE中,我將這串XML返回並嵌入了轉義字符,例如雙引號是「 」 而不是 「 有沒有簡單的方法來刪除轉義的字符序列項?
謝謝, 德里克刪除轉義字符

回答

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 
+0

我喜歡這種方式,因爲它直接。但是,至少在IE 9中似乎不起作用。在IE中打開你的jsFiddle,看看你是否得到和我一樣的問題。 –

+0

似乎只有一個document.write在ie中使用jsfiddle時...在這裏修復:http://jsfiddle.net/GSUES/ –

+0

@JanSommer:謝謝,我用你的小提琴更新了我的答案。 – Travesty3

1

之前試圖解決這一問題,您應該調查正在被替換的其它字符。例如,當您在其他瀏覽器中獲得單個\時,您會在IE中獲得\\嗎?

如果添加標準C逃逸,然後將JSON.parse轉換序列等\""\\\\n成線料,等等

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

JSON.parse被支撐本身上最新的瀏覽器,並在IE上專門回到IE 8。relevant MSDN page

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.

+0

你好,正在替換的其他字符是\ t的標籤,並且換行符爲\ n。而這隻發生在IE實際上不是Chrome或Firefox。我想嘗試使用JSON.parse。我是否需要包含特定的庫才能使用它? –

+0

@geoderek,是否需要特定的庫取決於IE的版本。 [MSDN的JSON頁面](http://msdn.microsoft.com/en-us/library/cc836466%28v=vs.94%29.aspx)說'JSON.parse'是「在以下文檔模式下支持:Internet Explorer 8標準,Internet Explorer 9標準,Internet Explorer 10標準。「如果您在IE 6或IE 7上遇到同樣的問題,那麼https://github.com/douglascrockford/JSON-js或JSON.org的其他解析器之一應該可以工作。 –

+0

好吧,我正在嘗試它。代碼行是JSON.parse(strInfo),它返回錯誤:無效字符。變量strInfo是包含轉義字符的XML字符串。我猜測我沒有正確使用JSON.parse。 –