I want to display text to HTML by a JavaScript function. How can I escape HTML special characters in JavaScript? Is there an API?
function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }
2.1m questions
2.1m answers
60 comments
57.0k users