Escape HTML Entities for Output
Escape untrusted text before inserting it into HTML so angle brackets and quotes cannot break out of the surrounding context.
Code
const ENTITIES = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
export function escapeHtml(text) {
if (text === null || text === undefined) return "";
return String(text).replace(/[&<>"']/g, (char) => ENTITIES[char]);
}Report a problem with this exampleOpens a pre-filled issue on GitHub.