Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T20:12:38.793Z
Answers.Text: Glad there is a sensible explanation... is this documented anywhere for future reference?
Answers.UserDisplayName: Neal Hulme
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T20:29:35.163Z
Answers.Text: Is now! http://buildwithcraft.com/docs/templating/filters#replace
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T20:46:21.980Z
Answers.Text: Awesome, thanks Brad!
Answers.UserDisplayName: Neal Hulme
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-16T18:20:00.407Z
Answers.Text: 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.
Answers.UserDisplayName: Marcos Buarque
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T20:04:37.200Z
Answers.LastActivityDate: 2014-12-15T03:29:28.957Z
Answers.LastEditDate: 2014-12-15T03:29:28.957Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-21T19:23:46.773Z
Comments.Text: Meant to say the docs show only single backslashes hence the confusion...
Comments.UserDisplayName: Neal Hulme
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-22T07:38:15.160Z
Comments.Text: 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).
Comments.UserDisplayName: carlcs
Tags: <templating><regex><replace>
Title: Double escaping Regular Expressions in Craft's Twig replace filter?
ThreadText: # 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**: username_0, 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 username_2!  
  > **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.  
  
  ---
  
  