Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created June 21, 2016 11:34
Show Gist options
  • Save akirattii/f7ccc57a799f851b1cdabc5262927b29 to your computer and use it in GitHub Desktop.
Save akirattii/f7ccc57a799f851b1cdabc5262927b29 to your computer and use it in GitHub Desktop.
HTML Sanitizer
const Sanitizer = {
encode: function(str) {
if (!str) return;
str = "" + str;
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
},
decode: function(str) {
if (!str) return;
str = "" + str;
return str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, '\'').replace(/&amp;/g, '&');
}
};
var encoded = Sanitizer.encode("<html>");
var decoded = Sanitizer.decode(encoded);
console.log(encoded, decoded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment