Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active October 21, 2023 16:40
Show Gist options
  • Save hubgit/9a9f1545e91a4c19f97e52985900a30d to your computer and use it in GitHub Desktop.
Save hubgit/9a9f1545e91a4c19f97e52985900a30d to your computer and use it in GitHub Desktop.
const escapeHTML = input => input.replace(/[<>&"']/g, char => `&#${char.charCodeAt(0)};`)
# https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML
const entities = new Map([
[`<`, `&lt;`],
[`>`, `&gt;`],
[`&`, `&amp;`],
[`"`, `&quot;`],
[`'`, `&apos;`],
])
export const escapeHTML = input => input.replace(/[<>&"']/g, char => entities.get(char))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment