Unescape HTML entities in Javascript?

前端 未结 30 2976
野趣味
野趣味 2020-11-21 05:40

I have some Javascript code that communicates with an XML-RPC backend. The XML-RPC returns strings of the form:


30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 05:57

    I use this in my project: inspired by other answers but with an extra secure parameter, can be useful when you deal with decorated characters

    var decodeEntities=(function(){
    
        var el=document.createElement('div');
        return function(str, safeEscape){
    
            if(str && typeof str === 'string'){
    
                str=str.replace(/\

    And it's usable like:

    var label='safe  character éntity';
    var safehtml='
    '+decodeEntities(label, true)+'
    ';

提交回复
热议问题