escapeHtml
EditEscapes characters with special meaning in HTML text and attributes.
const entities: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'",};
/** Escapes the five characters with special meaning in HTML text and attributes. */export const escapeHtml = (value: string): string => { return value.replace( /[&<>"']/g, (character) => entities[character] ?? character );};Download
Section titled “Download”wget -O src/lib/escapeHtml.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/escapeHtml.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/functions "$temp_dir" && mv "$temp_dir/escapeHtml.ts" src/lib/escapeHtml.ts && rm -r "$temp_dir"Examples
Section titled “Examples”import { escapeHtml } from "./escapeHtml";
const safe = escapeHtml(`<p title="Tom & Jerry">`);value(string) — Untrusted text.- Escapes
&,<,>, double quotes, and single quotes. - This does not sanitize complete HTML documents.