Last active
October 21, 2023 16:40
-
-
Save hubgit/9a9f1545e91a4c19f97e52985900a30d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const escapeHTML = input => input.replace(/[<>&"']/g, char => `&#${char.charCodeAt(0)};`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML | |
const entities = new Map([ | |
[`<`, `<`], | |
[`>`, `>`], | |
[`&`, `&`], | |
[`"`, `"`], | |
[`'`, `'`], | |
]) | |
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