Decode HTML & # 209 entities & &

6

I'm using the JQuery autocomplete library. I currently have a function that decodes HTML entities of numeric type - > Ñ , but what I do not know is how to integrate entities of this type into the function - > & .

My function:

function decodificarEntidadesHTMLNumericas(texto) {
    return texto.replace(/&#(\d{1,8});/g, function(m, ascii) {
        return String.fromCharCode(ascii);
    });
}

I do not know how to integrate the regular expression so that it decodes both cases.

    
asked by Ivxn 24.11.2016 в 20:47
source

3 answers

5

It can be done in 2 ways:

  • Using the DOM , and creating a textarea that we use for the browser to take care of everything. Its advantage is that it allows decoding all cases:

    function decodificarEntidadesHTML(html) {
        var texto = document.createElement("textarea");
        texto.innerHTML = html;
        return texto.value;
    }
    


    Or, the same code, but avoiding creating the textarea again and again:

    decodificarEntidadesHTML = (function(html) {
        var texto;
        return function(html){
            texto = texto || document.createElement("textarea");
            texto.innerHTML = html;
            return texto.value;
        }
    })();
    

  • Edit the function just to replace the cases that interest you. It has the advantage that it is done directly with a function, it does not need to be executed in a browser, and it avoids creating elements in the DOM. As a disadvantage, it does not cover all cases, or it would be tedious to incorporate the complete list of entities in the function:

    function decodificarEntidadesHTML(texto) {
        return texto.replace(/&(?:#(?:(\d{1,8})|x([a-z0-9]{1,8}))|(\w+));/gi, function(m, ascii, hex, entidad) {
            if (entidad) {
                switch (entidad) {
                    case "amp":
                        return "&";
                    case "aacute":
                        return "á";
                    //agregar otros...
                    default:
                        return "&" + entidad + ";";
                }
            } else {
                var dec = parseInt(ascii,10) || parseInt(hex,16);
                return String.fromCharCode(dec);
            }
        });
    }
    
  • answered by 24.11.2016 / 21:03
    source
    4

    Response taken from link

     function decode(str) {
       var elem = document.createElement('textarea');
       elem.innerHTML = str;
       return elem.value;
     }
     console.log(decode("&Ñ"))

    They recommend using a textarea instead of a div to mitigate XSS vulnerabilities.

        
    answered by 24.11.2016 в 21:11
    4

    Using jQuery :

    function decodificarEntidadesHTML (texto) {
      return $('<textarea \>').html(texto).text();
    }
    

    Example of use:

    function decodificarEntidadesHTML (texto) {
      return $('<textarea \>').html(texto).text();
    }
    
    // 
    $('#entrada').val('Bonnie<b>&amp;</b>Cliet');
    
    $('#traducir').on('click', function(){
      var resultado = decodificarEntidadesHTML($('#entrada').val());
      $('#resultado').text(resultado);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    Traducir: <textarea id="entrada"></textarea><br/>
    <button type="button" id="traducir">Traducir</button><br/>
    Traducción: <div id="resultado" style="display: inline"></div>
        
    answered by 24.11.2016 в 21:05