개요
- JavaScript escapeHtml()
function escapeHtml(str) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}
console.log( escapeHtml("<a href='test'>Test</a>") );
// <a href='test'>Test</a>
console.log( escapeHtml("This is some <b>bold</b> text.") );
// This is some <b>bold</b> text.