<p>Twig auto-escapes the <code>\</code> character so by the time it makes it to the Craft <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/twigextensions/CraftTwigExtension.php#L140" rel="nofollow">replace</a> filter PHP code, the regex string looks like <code>/^d+.s/</code>.</p>

<p>By double escaping it, you're forcing the correct regex to make it through <code>/^\d+\.\s/</code>.</p>

CC BY-SA 3.0
2014-10-21T20:12:38.793Z
Glad there is a sensible explanation... is this documented anywhere for future reference?
Neal Hulme
CC BY-SA 3.0
2014-10-21T20:29:35.163Z
Is now! http://buildwithcraft.com/docs/templating/filters#replace
Brad Bell
CC BY-SA 3.0
2014-10-21T20:46:21.980Z
Awesome, thanks Brad!
Neal Hulme
CC BY-SA 4.0
2022-05-16T18:20:00.407Z
This is not a Craft-CMS related comment, but I want to say I could only use the `matches` operator in TWIG to validate a string against a regular expression when I used double-escaping in my regexp.
Marcos Buarque
CC BY-SA 3.0
2014-10-21T20:04:37.200Z
2014-12-15T03:29:28.957Z
2014-12-15T03:29:28.957Z
Brad Bell
<p>Just trying to use Craft's Twig replace filter to remove number prefixes from the beginning of User Groups names and I have had to double escape the character classes etc to get it to work. I tried this...</p>

<pre><code>{{ group.name|replace('/^\d+\.\s/','') }}{% if not loop.last %}, {% endif %}
</code></pre>

<p>and it didn't work so for some reason I tried this...</p>

<pre><code>{{ group.name|replace('/^\\d+\\.\\s/','') }}{% if not loop.last %}, {% endif %}
</code></pre>

<p>...and it worked. Is there a sensible explanation for this as Regular Expressions are like Voodoo?!</p>

CC BY-SA 3.0
2014-10-21T19:23:46.773Z
Meant to say the docs show only single backslashes hence the confusion...
Neal Hulme
CC BY-SA 3.0
2014-10-22T07:38:15.160Z
Neal, I came across the same mystery using Twig's `matches` operator, which was also wrong in [Sensiolabs' docs](http://twig.sensiolabs.org/doc/templates.html#comparisons).
carlcs
<templating><regex><replace>
Double escaping Regular Expressions in Craft's Twig replace filter?
# Double escaping Regular Expressions in Craft's Twig replace filter?

(Asked by: username_0 on 2014-10-21)

Just trying to use Craft's Twig replace filter to remove number prefixes from the beginning of User Groups names and I have had to double escape the character classes etc to get it to work. I tried this...
{{ group.name|replace('/^\d+\.\s/','') }}{% if not loop.last %}, {% endif %}

and it didn't work so for some reason I tried this...
{{ group.name|replace('/^\\d+\\.\\s/','') }}{% if not loop.last %}, {% endif %}

...and it worked. Is there a sensible explanation for this as Regular Expressions are like Voodoo?!

**Tags:** templating, regex, replace

> **Comments:**
> **username_0**: Meant to say the docs show only single backslashes hence the confusion...  
> **username_1**: Neal, I came across the same mystery using Twig's `matches` operator, which was also wrong in [Sensiolabs' docs](http://twig.sensiolabs.org/doc/templates.html#comparisons).  

---

## 1 answers

### Answer by username_2 on 2014-10-21. Score: 5 | Accepted answer

Twig auto-escapes the `\` character so by the time it makes it to the Craft [replace](https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/twigextensions/CraftTwigExtension.php#L140) filter PHP code, the regex string looks like `/^d+.s/`.

By double escaping it, you're forcing the correct regex to make it through `/^\d+\.\s/`.

> **Comments:**
> **username_0**: Glad there is a sensible explanation... is this documented anywhere for future reference?  
> **username_2**: Is now! http://buildwithcraft.com/docs/templating/filters#replace  
> **username_0**: Awesome, thanks Brad!  
> **username_3**: This is not a Craft-CMS related comment, but I want to say I could only use the `matches` operator in TWIG to validate a string against a regular expression when I used double-escaping in my regexp.  

---

