Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-15T02:53:17.043Z
Answers.LastActivityDate: 2016-04-15T02:53:17.043Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <regex><wysiwyg>
Title: Using 'replace' with a regex is throwing an error. Any ideas why?
ThreadText: # 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 `<p>` tags around `<img>` tags that are automatically getting generated in a WYSIWYG field.
  
  **The error:** preg\_replace(): `Unknown modifier '&gt;'`
  
  ```
  {{ story.html|raw|replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\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("/<p>\\s*(<a .*>)?\\s*(<img .* \\/>)\\s*(<\\/a>)?\\s*<\\/p>/iU", "\1\2\3") }}
  
  ```
  
  
  ---
  
  