Skip to content

Instantly share code, notes, and snippets.

@kfriend
Created June 2, 2014 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfriend/a377c70a6fd7001718bf to your computer and use it in GitHub Desktop.
Save kfriend/a377c70a6fd7001718bf to your computer and use it in GitHub Desktop.
JavaScript: HTML encoder
escapeHTML = (function() {
var entities = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2F;'
};
var regex = /[&<>"'\/]/g;
return function(string) {
return ('' + string).replace(regex, function(match) {
return entities[match];
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment