All of the re-assignments of value is unnecessary,  \s matches [ \t\n\r\f\v], see http://docs.python.org/2/library/re.html. That removes the first 2, then you can simply return the result of the regular expression, see https://github.com/django/django/blob/master/django/utils/html.py#L163-165, no assignments needed at all.