Skip to content

Instantly share code, notes, and snippets.

@baniol
Created December 3, 2014 09:06
Show Gist options
  • Save baniol/9e44027b41012af73921 to your computer and use it in GitHub Desktop.
Save baniol/9e44027b41012af73921 to your computer and use it in GitHub Desktop.
sanitize html in javascript. http://stackoverflow.com/a/10772475
String.prototype.sanitizeHTML=function (white,black) {
if (!white) white="b|i|p|br";//allowed tags
if (!black) black="script|object|embed";//complete remove tags
e=new RegExp("(<("+black+")[^>]*>.*</\\2>|(?!<[/]?("+white+")(\\s[^<]*>|[/]>|>))<[^<>]*>|(?!<[^<>\\s]+)\\s[^</>]+(?=[/>]))", "gi");
return this.replace(e,"");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment