This is similar to Can I escape the special html characters in javascript?
Accepted answer:
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
However, if you are using lodash, I like the cs01 answer from this post:
_.escape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
source
share