JavaScript escapeHtml()
by JM note
HTML
<script src='//rawgit.com/eu81273/jsfiddle-console/master/console.js
'></script>
<script>
function escapeHtml(str) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}
console.log( escapeHtml("<a href='test'>Test</a>") );
console.log( escapeHtml("This is some <b>bold</b> text.") );
// <a href='test'>Test</a>
// This is some <b>bold</b> text.
</script>