Skip to content

Instantly share code, notes, and snippets.

@sstur
Last active October 11, 2015 21:38
Show Gist options
  • Save sstur/3923135 to your computer and use it in GitHub Desktop.
Save sstur/3923135 to your computer and use it in GitHub Desktop.
Simple RegExp-based HTML Sanitization
(function() {
"use strict";
var RE_COMMENT = /<!--(.*?)-->/g;
var RE_SCRIPT = /<script[\s>](.*?)<\/script\s*>/ig;
//simple regex-based cleaning to strip scripts and comments
function htmlClean(html) {
var last, clean = (html == null) ? '' : String(html);
//repeatedly clean markup until cleaning has no more effect
while (last !== clean) {
last = clean;
clean = clean.replace(RE_COMMENT, '');
clean = clean.replace(RE_SCRIPT, '');
}
return clean;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment