Apparently, this is harder to find than I thought it would be. And it even is so simple...
Is there a function equivalent to PHP\'s htmlspecialchars built into Javas
function htmlEscape(str){
return str.replace(/[&<>'"]/g,x=>''+x.charCodeAt(0)+';')
}
This solution uses the numerical code of the characters, for example <
is replaced by <
.
Although its performance is slightly worse than the solution using a map, it has the advantages: