Using 'replace' with a regex is throwing an error. Any ideas why? I am attempting to strip out &lt;p&gt; tags around &lt;img&gt; tags that are automatically getting generated in a WYSIWYG field.

The error: preg_replace(): Unknown modifier '&amp;gt;'

{{ 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') }}


Thanks!
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("/&lt;p&gt;\\s*(&lt;a .*&gt;)?\\s*(&lt;img .* \\/&gt;)\\s*(&lt;\\/a&gt;)?\\s*&lt;\\/p&gt;/iU", "\1\2\3") }}