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

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 Sep 21 '17 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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