import re from django.template.defaultfilters import stringfilter from django.template import Library register = Library() @register.filter @stringfilter def killtags(value, tags): tags = [re.escape(tag) for tag in tags.split()] tags_re = u'(%s)' % u'|'.join(tags) kill_re = re.compile("<\s*%s[^>]*>(.*?)<\s*/\s*\\1>" % tags_re, re.U) value = kill_re.sub('', value) return value killtags.is_safe = True