I am using the JQuery autocomplete library. Currently I have a function that decodes HTML entities of type numeric -> Ñ
, but what I don't 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 don't know how to integrate the regular expression so that it decodes both cases.
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. It has the advantage that it allows decoding all cases:
Or, the same code, but avoiding creating the textarea over and over again:
Edit the function only 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, that it does not cover all cases, or that it would be tedious to incorporate the complete list of entities in the function:
Using
jQuery
:Usage example:
Answer taken from https://stackoverflow.com/a/3700369/1845602
They recommend using a textarea instead of a div to mitigate XSS vulnerabilities.