Created
August 10, 2017 18:03
-
-
Save mscalora/1bdd03289ba9dd041766a68e32eb2791 to your computer and use it in GitHub Desktop.
ES6ish htmlEncode
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 escapeChars = { '¢' : 'cent', '£' : 'pound', '¥' : 'yen', '€': 'euro', '©' : 'copy', '®' : 'reg', '<' : 'lt', '>' : 'gt', '"' : 'quot', '&' : 'amp', '\'' : '#39'}, | |
htmlEncode = function (s) { | |
return s.replace(/[\u00A0-\u9999<>\&'"]/gim, function (i) { | |
return `&${escapeChars[i] || '#' + i.charCodeAt(0)};`; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment