Escape HTML
VerifiedEscapes &, <, >, " and ' so untrusted text can be safely inserted into HTML.
- html
- security
- xss
- sanitize
Install
npx shadcn@latest add https://quicksnip.dev/r/escape-html.jsonCode
const ENTITIES: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
/** Escapes the five HTML-significant characters so text can be safely inserted into markup. */
export function escapeHtml(input: string): string {
return input.replace(/[&<>"']/g, (ch) => ENTITIES[ch]);
}Usage
escapeHtml('<b>"Tom" & Jerry</b>'); // "<b>"Tom" & Jerry</b>"