<p>I'm not sure what's going on with the <code>story.html</code> part, but you'll need to double escape your regex.</p>

<p>Assuming you have an entry already with a <code>story</code> Rich Text field, it'd look like this:</p>

<pre><code>{{ entry.body|raw|replace("/&lt;p&gt;\\s*(&lt;a .*&gt;)?\\s*(&lt;img .* \\/&gt;)\\s*(&lt;\\/a&gt;)?\\s*&lt;\\/p&gt;/iU", "\1\2\3") }}
</code></pre>

CC BY-SA 3.0
2016-04-15T02:53:17.043Z
2016-04-15T02:53:17.043Z
Brad Bell
<p>I am attempting to strip out <code>&lt;p&gt;</code> tags around <code>&lt;img&gt;</code> tags that are automatically getting generated in a WYSIWYG field.</p>

<p><strong>The error:</strong> preg_replace(): <code>Unknown modifier '&amp;gt;'</code></p>

<pre><code>{{ story.html|raw|replace('/&lt;p&gt;\s*(&lt;a .*&gt;)?\s*(&lt;img .* \/&gt;)\s*(&lt;\/a&gt;)?\s*&lt;\/p&gt;/iU','\1\2\3') }}
</code></pre>

<p>Thanks!</p>

<regex><wysiwyg>
Using 'replace' with a regex is throwing an error. Any ideas why?
# Using 'replace' with a regex is throwing an error. Any ideas why?

(Asked by: username_0 on 2016-04-14)

I am attempting to strip out  tags around  tags that are automatically getting generated in a WYSIWYG field.
The error: preg_replace(): Unknown modifier '>'
{{ story.html|raw|replace('/\s*()?\s*()\s*(<\/a>)?\s*<\/p>/iU','\1\2\3') }}

Thanks!

**Tags:** regex, wysiwyg

---

## 1 answers

### Answer by username_1 on 2016-04-15. Score: 1

I'm not sure what's going on with the story.html part, but you'll need to double escape your regex.
Assuming you have an entry already with a story Rich Text field, it'd look like this:
{{ entry.body|raw|replace("/\\s*()?\\s*()\\s*(<\\/a>)?\\s*<\\/p>/iU", "\1\2\3") }}


---

