Decode HTML & # 209 entities & &


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

3 answers


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...
                        return "&" + entidad + ";";
            } else {
                var dec = parseInt(ascii,10) || parseInt(hex,16);
                return String.fromCharCode(dec);
  • answered by 24.11.2016 / 21:03

    Response taken from link

     function decode(str) {
       var elem = document.createElement('textarea');
       elem.innerHTML = str;
       return elem.value;

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

    answered by 24.11.2016 в 21:11

    Using jQuery :

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

    Example of use:

    function decodificarEntidadesHTML (texto) {
      return $('<textarea \>').html(texto).text();
    $('#traducir').on('click', function(){
      var resultado = decodificarEntidadesHTML($('#entrada').val());
    <script src=""></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