2014-11-24 24 views
0

我必須在jQuery中使用xml文件,並且jQuery不能與<,>,&和其他代碼一起使用。我發現在谷歌這樣的代碼:恢復jQuery函數,從xml到html並返回

function escapeHtml(text) { 
    var map = { 
     '&': '&amp;', 
     '<': '&lt;', 
     '>': '&gt;', 
    }; 
    return text.replace(/[&<>]/g, function(m) { 
     return map[m]; 
    }); 
} 

它的工作原理,但現在我需要導出該文件,並將其與&lt;&gt;作爲文本返回,有沒有辦法恢復該回來嗎?

+0

@RoryMcCrossan替換功能未啓用與< > – 2014-11-24 15:29:04

回答

0

您只需通過交換鍵和對象,並使用Object.keys

function toHTML(text) { 
    var map = { 
     '&amp;': '&', 
     '&lt;': '<', 
     '&gt;': '>', 
    }; 
    return text.replace(new RegExp(Object.keys(map).join("|"),"g"), function(m) { 
     return map[m]; 
    }); 
} 
+0

由於工作像這樣做,它的工作對我來說:3 – 2014-11-24 15:37:41

+0

@RaduDascălu高興它幫助。如果有幫助,將其標記爲答案 – 2014-11-24 15:38:29