Skip to content

Instantly share code, notes, and snippets.

@mislav
Created February 10, 2009 18:52
Show Gist options
  • Save mislav/61530 to your computer and use it in GitHub Desktop.
Save mislav/61530 to your computer and use it in GitHub Desktop.
advanced String#stripTags() with whitelisting
String.prototype.stripTags = String.prototype.stripTags.wrap(
function(proceed, allowTags) {
if (allowTags) {
if (Object.isString(allowTags)) allowTags = $w(allowTags)
this.gsub(/(<\/?\s*)([^\s>]+)(\s[^>]*)?>/, function(match) {
if (allowTags.include(match[2].toLowerCase()))
return match[1] + match[2] + match[3] + '>'
})
} else {
// proceed using the original function
return proceed();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment