I need the most efficient way to convert all applicable characters to HTML entities.
Two of the solutions I have found on stack overflow are as follow, but one of these involves using DOM and other regular expression.
Using Regular Expression:
return mystring.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
Using DOM:
function HtmlEncode(s)
{
var el = document.createElement("div");
el.innerText = el.textContent = s;
s = el.innerHTML;
delete el;
return s;
}
Do anyone of you know the efficient solution to convert characters to HTML entities without using any DOM API and regular expression?