Skip to content

Instantly share code, notes, and snippets.

@nathanielmata
Last active October 4, 2020 17:38
Show Gist options
  • Save nathanielmata/4d2045a978642f57dcd29deca29751ff to your computer and use it in GitHub Desktop.
Save nathanielmata/4d2045a978642f57dcd29deca29751ff to your computer and use it in GitHub Desktop.
replace reserved characters with HTML Entity
function escapeHtml(unsafe) {
const unsafeCharList = {
"&": "&",
"<": "&lt;",
">": "&gt;",
"'": "&#039;",
'"': "&quot;",
}
return unsafe.replace(/[&<>\'"']/g, function(match) {
return unsafeCharList[match];
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment