<p>You were close!</p>

<p>Just don't use the Twig hash/array syntax when using the <code>|replace</code> filter if you want Craft to process it as regex.  This should do what you're looking for:</p>

<pre><code>{% set svg_image_output = svg_image|replace('/#[a-zA-Z0-9]{6}|rgb\\((?:\\s*\\d+\\s*,){2}\\s*[\\d]+\\)|rgba\\((\\s*\\d+\\s*,){3}[\\d\\.]+\\)|hsl\\(\\s*\\d+\\s*(\\s*\\,\\s*\\d+\\%){2}\\)|hsla\\(\\s*\\d+(\\s*,\\s*\\d+\\s*\\%){2}\\s*\\,\\s*[\\d\\.]+\\)/', '****') %}
</code></pre>

CC BY-SA 4.0
2018-06-04T23:09:17.577Z
2018-06-04T23:09:17.577Z
Brad Bell
<p>I am trying to change all rgb colours in a bit of text/content. I have the RegEx working here in php using preg_replace:</p>

<pre><code>preg_replace("/#[a-zA-Z0-9]{6}|rgb\((?:\s*\d+\s*,){2}\s*[\d]+\)|rgba\((\s*\d+\s*,){3}[\d\.]+\)|hsl\(\s*\d+\s*(\s*\,\s*\d+\%){2}\)|hsla\(\s*\d+(\s*,\s*\d+\s*\%){2}\s*\,\s*[\d\.]+\)/", "*****", $input_lines);
</code></pre>

<p><a href="https://www.phpliveregex.com/p/oc2" rel="nofollow noreferrer">https://www.phpliveregex.com/p/oc2</a></p>

<p>But cannot seem to get it working in craft template code am trying is like this:</p>

<pre><code>{% set svg_image = '
#ffffff
#ffffffasdf
asdf#ffffff
rgba(170,221,255,0.59)
rgba(170,221,255,0.59)asdf
rgb(0, 0, 0)
rgb(33, 44,66)
rgb(0,0,0)asdf
rgba(170,221,255,0.59dsaf)
' %} 

{% set svg_image_output = svg_image|replace({ '/#[a-zA-Z0-9]{6}|rgb\\((?:\\s*\\d+\\s*,){2}\\s*[\\d]+\\)|rgba\\((\\s*\\d+\\s*,){3}[\\d\\.]+\\)|hsl\\(\\s*\\d+\\s*(\\s*\\,\\s*\\d+\\%){2}\\)|hsla\\(\\s*\\d+(\\s*,\\s*\\d+\\s*\\%){2}\\s*\\,\\s*[\\d\\.]+\\)/': '****' }) %}
{{ svg_image_output | raw }}
</code></pre>

<p>I have:
 - added double-escaped '\' as the docs suggest in v2
 - started with / and ended with /</p>

<p>Seems in the docs that if this is done it works like preg_replace.</p>

<p>Any ideas?</p>

<regex>
Craft 3 RegEx problem
# Craft 3 RegEx problem

(Asked by: username_0 on 2018-06-04)

I am trying to change all rgb colours in a bit of text/content. I have the RegEx working here in php using preg_replace:
preg_replace("/#[a-zA-Z0-9]{6}|rgb\((?:\s*\d+\s*,){2}\s*[\d]+\)|rgba\((\s*\d+\s*,){3}[\d\.]+\)|hsl\(\s*\d+\s*(\s*\,\s*\d+\%){2}\)|hsla\(\s*\d+(\s*,\s*\d+\s*\%){2}\s*\,\s*[\d\.]+\)/", "*****", $input_lines);

https://www.phpliveregex.com/p/oc2
But cannot seem to get it working in craft template code am trying is like this:
{% set svg_image = '
#ffffff
#ffffffasdf
asdf#ffffff
rgba(170,221,255,0.59)
rgba(170,221,255,0.59)asdf
rgb(0, 0, 0)
rgb(33, 44,66)
rgb(0,0,0)asdf
rgba(170,221,255,0.59dsaf)
' %} 

{% set svg_image_output = svg_image|replace({ '/#[a-zA-Z0-9]{6}|rgb\\((?:\\s*\\d+\\s*,){2}\\s*[\\d]+\\)|rgba\\((\\s*\\d+\\s*,){3}[\\d\\.]+\\)|hsl\\(\\s*\\d+\\s*(\\s*\\,\\s*\\d+\\%){2}\\)|hsla\\(\\s*\\d+(\\s*,\\s*\\d+\\s*\\%){2}\\s*\\,\\s*[\\d\\.]+\\)/': '****' }) %}
{{ svg_image_output | raw }}

I have:
 - added double-escaped '\' as the docs suggest in v2
 - started with / and ended with /
Seems in the docs that if this is done it works like preg_replace.
Any ideas?

**Tags:** regex


---

## 1 answers

### Answer by username_1 on 2018-06-04. Score: 2 | Accepted answer

You were close!
Just don't use the Twig hash/array syntax when using the |replace filter if you want Craft to process it as regex.  This should do what you're looking for:
{% set svg_image_output = svg_image|replace('/#[a-zA-Z0-9]{6}|rgb\\((?:\\s*\\d+\\s*,){2}\\s*[\\d]+\\)|rgba\\((\\s*\\d+\\s*,){3}[\\d\\.]+\\)|hsl\\(\\s*\\d+\\s*(\\s*\\,\\s*\\d+\\%){2}\\)|hsla\\(\\s*\\d+(\\s*,\\s*\\d+\\s*\\%){2}\\s*\\,\\s*[\\d\\.]+\\)/', '****') %}


---

