JavaScript escapeHtml()

by JM note

HTML

<script src='//rawgit.com/eu81273/jsfiddle-console/master/console.js
'></script>
<script>
function escapeHtml(str) {
	var map = {
		'&': '&amp;',
		'<': '&lt;',
		'>': '&gt;',
		'"': '&quot;',
		"'": '&#039;'
	};
	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.") );
// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
// This is some &lt;b&gt;bold&lt;/b&gt; text.
</script>