Created
November 5, 2014 17:14
-
-
Save thetallweeks/7c452e211f286e77b6f2 to your computer and use it in GitHub Desktop.
Little function for escaping html characters (in TypeScript)
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
// From Tom Gruner @ http://stackoverflow.com/a/12034334/1660815 | |
var entityMap = { | |
"&": "&", | |
"<": "<", | |
">": ">", | |
'"': '"', | |
"'": ''', | |
"/": '/' | |
}; | |
function escapeHtml(source: string) { | |
return String(source).replace(/[&<>"'\/]/g, s => entityMap[s]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A null-check-resistant variant: