import re from django.utils.encoding import smart_unicode r_lightbox = re.compile('<a (?=[^>]*\.(jpg|gif|png))(?![^>]*lightbox)') s_lightbox = '<a rel="lightbox" ' @register.filter def lightbox(content): return r_lightbox.sub(s_lightbox, smart_unicode(content))