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);
}
});
}