Skip to content

Instantly share code, notes, and snippets.

@mscalora
Created August 10, 2017 18:03
Show Gist options
  • Save mscalora/1bdd03289ba9dd041766a68e32eb2791 to your computer and use it in GitHub Desktop.
Save mscalora/1bdd03289ba9dd041766a68e32eb2791 to your computer and use it in GitHub Desktop.
ES6ish htmlEncode
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