IE XDomainRequest responseText和HTML

时间:2012-04-11 01:06:36

标签: ajax regex internet-explorer-8 xdomainrequest

我在IE 8中使用XDomainRequest来获取网页的内容。 reponseText包含转义字符和unicode字符,可防止将标记插入div。这是返回数据的一个示例。

<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">\u000d\u000a<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\u000d\u000a<head>\u000d\u000a    <title>...<\/title>\u000d\u000a    <script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js\" type=\"text\/javascript\"><\/script>\u000d\u000a<\/head>\u000d\u000a<body>\u000d\u000a\u000d\u000a<div style=\"font-size:24px;font-weight:bold\">\u000d\u000aText Headline: \u000d\u000a<\/div>\u000d\u000a\u000d\u000a<div style=\"float:left;width:50%;margin:0;padding:0;\">\u000d\u000a<p>Lorem ipsum dolor sit amet<\/p>\u000d\u000a\u000d\u000a<p>In nec imperdiet lectus. 

当我使用decodeURI或decodeURIComponent时,我得到“要解码的URI不是有效编码”错误。

有人可以建议使用方法或正则表达式来清理HTML吗?

1 个答案:

答案 0 :(得分:-1)

基于我的快速测试:

regex = /\\([^u])/g;
// put it in a JSON object so that JS doesn't automatically de-escape
string = JSON.stringify({response: '<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">\u000d\u000a<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\u000d\u000a<head>\u000d\u000a    <title>...<\/title>\u000d\u000a    <script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js\" type=\"text\/javascript\"><\/script>\u000d\u000a<\/head>\u000d\u000a<body>\u000d\u000a\u000d\u000a<div style=\"font-size:24px;font-weight:bold\">\u000d\u000aText Headline: \u000d\u000a<\/div>\u000d\u000a\u000d\u000a<div style=\"float:left;width:50%;margin:0;padding:0;\">\u000d\u000a<p>Lorem ipsum dolor sit amet<\/p>\u000d\u000a\u000d\u000a<p>In nec imperdiet lectus.'});

string.replace(regex, '$1');

将替换除Unicode之外的所有转义斜杠。我认为JS中没有许多其他非转义用于正斜杠的用法。