Skip to content

Instantly share code, notes, and snippets.

@themeblvd
Last active July 3, 2018 14:53
Show Gist options
  • Save themeblvd/89bbd4d9dfdabab88a24bc56654f51a3 to your computer and use it in GitHub Desktop.
Save themeblvd/89bbd4d9dfdabab88a24bc56654f51a3 to your computer and use it in GitHub Desktop.
Simple JavaScript function to escape HTML.
/**
* Escape HTML.
*
* @param {String} html HTML code.
* @return {String} Escaped html.
*/
function escape(html) {
const replace = {
'&': '&',
'<': '&lt;',
'>': '&gt;'
};
return html.replace(/[&<>]/g, tag => replace[tag]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment