<p>Something like this should work:</p>

<pre><code>{{ snippet|replace('/&lt;img([\w\W]+?)&gt;/', '') }}
</code></pre>

<p>You can see an explanation of the regex in the right column of <a href="https://regex101.com/r/DvJHeY/1" rel="nofollow noreferrer">https://regex101.com/r/DvJHeY/1</a></p>

CC BY-SA 3.0
2017-09-21T18:59:58.303Z
Awesome, thanks again Brad!
jtenclay
CC BY-SA 3.0
2017-09-20T20:12:49.220Z
2017-09-20T20:12:49.220Z
Brad Bell
<p>I'm looking to format out image tags from a string using the <code>|replace</code> filter. The behavior I'm looking for is to give it a string like this:</p>

<p><code>&lt;img src="test.jpg"&gt;&lt;p&gt;some text&lt;/p&gt;&lt;img src="test.jpg"&gt;</code></p>

<p>and get back:</p>

<p><code>&lt;p&gt;some text&lt;/p&gt;</code></p>

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

<filter><regex>
|replace filter is matching too many characters
# |replace filter is matching too many characters

(Asked by: username_0 on 2017-09-20)

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:

`![](test.jpg)

some text

![](test.jpg)`

and get back:

`some text`

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

**Tags:** filter, regex

---

## 1 answers

### Answer by username_1 on 2017-09-20. Score: 1 | Accepted answer

Something like this should work:
{{ snippet|replace('//', '') }}

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

> **Comments:**
> **username_0**: Awesome, thanks again Brad!  

---

