Answers.Body: <p>You could try to use Retcon for that:</p>
  <p><a href="https://github.com/mmikkel/Retcon-Craft/blob/master/README.md" rel="nofollow noreferrer">https://github.com/mmikkel/Retcon-Craft/blob/master/README.md</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-31T15:07:23.617Z
Answers.LastActivityDate: 2021-08-31T15:07:23.617Z
Answers.UserDisplayName: Max Strebel
Body: <p>I'm attempting to capture 4 attributes of an Attributes of an  tag and injecting inline styles to the style attribute.  Craft CMS and Twig templates are being used to generate HTML emails here, hence the inline styling.</p>
  <p>This is my expression working in isolation: <a href="https://regexr.com/64a3f" rel="nofollow noreferrer">https://regexr.com/64a3f</a></p>
  <p>And here is my twig replace:</p>
  <pre><code>{% set styledText = styledText
    |replace('/&lt;a(?&gt;\s+(?:href=&quot;([^&quot;]*)&quot;|class=&quot;([^&quot;]*)&quot;|target=&quot;([^&quot;]*)&quot;|style=&quot;([^&quot;]*)&quot;)|[^\s&gt;]+|\s+)*&gt;/m', '&lt;a href=&quot;$1&quot; class=&quot;$2&quot; style=&quot;' ~ linkColor ~ textDecoration ~ '$4&quot; target=&quot;$3&quot;&gt;')
  %}
  </code></pre>
  <p>The above is spitting out: <code>&lt;a href=&quot;&quot; class=&quot;&quot; style=&quot;color:#0000ff;text-decoration:underline;&quot; target=&quot;&quot;&gt;</code></p>
  <p>So it appears all my capture groups are empty.  The attribute values are not being carried over in the replace and I can't figure out why.</p>
  <p>I have some simpler regular expressions working in Twig that just deal with class and style attributes.</p>
  <p>I'm using Craft 3.5.x so the issue shouldn't be needing double backslashes.  In fact, trying causes an internal server error.</p>
  <p>If anyone has any thoughts that could help me get to the bottom of this I would greatly appreciate it.</p>
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-24T08:14:07.413Z
Comments.Text: That's a classic. You start out with one problem, try to solve it with regex – now you have two problems :)  Since all you're doing is adding some inline styles, why not just use CSS? Or allow the editor of the styled text to add the styling themselves, somehow?
Comments.UserDisplayName: MoritzLost
Tags: <craft3><templating><regex>
Title: Regular Expression replace not working within Twig Template
ThreadText: # Regular Expression replace not working within Twig Template
  
  (Asked by: username_0 on 2021-08-24)
  
  I'm attempting to capture 4 attributes of an Attributes of an tag and injecting inline styles to the style attribute. Craft CMS and Twig templates are being used to generate HTML emails here, hence the inline styling.
  
  This is my expression working in isolation: <https://regexr.com/64a3f>
  
  And here is my twig replace:
  
  ```
  {% set styledText = styledText
    |replace('/<a(?>\s+(?:href="([^"]*)"|class="([^"]*)"|target="([^"]*)"|style="([^"]*)")|[^\s>]+|\s+)*>/m', '<a href="$1" class="$2" style="' ~ linkColor ~ textDecoration ~ '$4" target="$3">')
  %}
  
  ```
  
  The above is spitting out: `<a href="" class="" style="color:#0000ff;text-decoration:underline;" target="">`
  
  So it appears all my capture groups are empty. The attribute values are not being carried over in the replace and I can't figure out why.
  
  I have some simpler regular expressions working in Twig that just deal with class and style attributes.
  
  I'm using Craft 3.5.x so the issue shouldn't be needing double backslashes. In fact, trying causes an internal server error.
  
  If anyone has any thoughts that could help me get to the bottom of this I would greatly appreciate it.
  
  Thanks
  
  **Tags:** craft3, templating, regex
  
  > **Comments:**
  > **username_1**: That's a classic. You start out with one problem, try to solve it with regex – now you have two problems :)  Since all you're doing is adding some inline styles, why not just use CSS? Or allow the editor of the styled text to add the styling themselves, somehow?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-08-31. Score: 1
  
  You could try to use Retcon for that:
  
  <https://github.com/mmikkel/Retcon-Craft/blob/master/README.md>
  
  
  ---
  
  