Created
December 16, 2021 16:20
-
-
Save YaronMiro/ecbed7a531326fa906b9d90e74913a93 to your computer and use it in GitHub Desktop.
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