Is there a native way to encode or decode HTML entities using JavaScript or ES6? For example, <
would be encoded as <
. There are libraries like html-entities
for Node.js but it feels like there should be something built into JavaScript that already handles this common need.
A nice function using es6 for escaping html:
const escapeHTML = str => str.replace(/[&<>'"]/g,
tag => ({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag]));