1

I'm looking to format out image tags from a string using the |replace filter. The behavior I'm looking for is to give it a string like this:

<img src="test.jpg"><p>some text</p><img src="test.jpg">

and get back:

<p>some text</p>

Unfortunately, when I use {{ snippet|replace('/<img(.*)>/', '') }} it matches the entire string, from the opening <img to the second image's closing > rather than matching both <img> tags individually. How can I force the regex to match only through the first > it finds before continuing?

1 Answer 1

1

Something like this should work:

{{ snippet|replace('/<img([\w\W]+?)>/', '') }}

You can see an explanation of the regex in the right column of https://regex101.com/r/DvJHeY/1

1
  • Awesome, thanks again Brad!
    – jtenclay
    Commented Sep 21, 2017 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.