How do I use regular expressions in Jinja2?

前端 未结 1 858
不思量自难忘°
不思量自难忘° 2020-12-29 03:26

I\'m new to Jinja2 and so far I\'ve been able to do most of what I want. However, I need to use regular expressions and I can\'t seem to find anything anywhere in the docume

相关标签:
1条回答
  • 2020-12-29 04:24

    There is an already existing filter called replace that you can use if you don't actually need a regular expression. Otherwise, you can register a custom filter:

    {# Replace method #}
    {{my_str|replace("some text", "")|replace(" ", "_")}}
    

     

    # Custom filter method
    def regex_replace(s, find, replace):
        """A non-optimal implementation of a regex filter"""
        return re.sub(find, replace, s)
    
    jinja_environment.filters['regex_replace'] = regex_replace
    
    0 讨论(0)
提交回复
热议问题