Created
December 16, 2021 16:20
Escape all quotes (single or double), text may include such characters. Good for dynamic text for HTML attributes.
This file contains hidden or 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
// We have 2 capture groups of each quote type, and than replace each on with | |
"h\"ell'o there".replace(/(")|(')/g, '\\$1$2'); | |
const escapeHtml = (unsafeText) => { | |
return unsafeText | |
.replaceAll('&', '&') | |
.replaceAll('<', '<') | |
.replaceAll('>', '>') | |
.replaceAll('"', '"') | |
.replaceAll("'", '''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment