Last active
October 11, 2015 21:38
-
-
Save sstur/3923135 to your computer and use it in GitHub Desktop.
Simple RegExp-based HTML Sanitization
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
(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