Answers.Body: <p>Try doubling-up your backslashes:</p>
  
  <p><a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow noreferrer">https://craftcms.com/docs/templating/filters#replace</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-18T08:47:17.837Z
Answers.Text: Good shout, that fixed it. Any idea why double back-slashes are required?
Answers.UserDisplayName: jerle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-18T20:08:35.443Z
Answers.Text: I don't know exactly for sure, but in general whenever you're passing one language through to another (in this case, passing a regex through twig), you need to double-escape if both languages share the same escape characters. Because the first language (twig in this case) will escape the backslash, but you want an actual backslash passed through to the regex (so it can then escape the character after it), so you need to tell twig "I want an actual backslash here to be sent to the regex". Or something like that.
Answers.UserDisplayName: Jordan Lev
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-13T16:21:25.163Z
Answers.LastActivityDate: 2017-04-13T16:21:25.163Z
Answers.UserDisplayName: Jordan Lev
Body: <p>As I understand, craft overrides Twig's <code>replace</code> filter to allow for the use of regex; which is just running <code>preg_replace</code> under the hood</p>
  
  <p>When running the following regex through <code>preg_replace</code> in a php page (outside of craft) in order to match non-ASCII characters; it works as expected:</p>
  
  <pre><code>/([^ !"#$%&amp;\'\(\)*\+,-\.\/0123456789:;&lt;=&gt;\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^\\\_`abcdefghijklmnopqrstuvwxyz{\|}~\n]+)/u
  </code></pre>
  
  <p>However, when running it through <code>replace</code> in a template in craft; nothing is matched</p>
  
  <p>Any ideas as to why this is happening and possible solutions?</p>
  
  <p>Thanks</p>
  
Tags: <templating><replace><regex>
Title: Twig replace regex
ThreadText: # Twig replace regex
  
  (Asked by: username_0 on 2017-04-13)
  
  As I understand, craft overrides Twig's `replace` filter to allow for the use of regex; which is just running `preg_replace` under the hood
  
  When running the following regex through `preg_replace` in a php page (outside of craft) in order to match non-ASCII characters; it works as expected:
  
  ```
  /([^ !"#$%&\'\(\)*\+,-\.\/0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^\\\_`abcdefghijklmnopqrstuvwxyz{\|}~\n]+)/u
  
  ```
  
  However, when running it through `replace` in a template in craft; nothing is matched
  
  Any ideas as to why this is happening and possible solutions?
  
  Thanks
  
  **Tags:** templating, replace, regex
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-04-13. Score: 4 | Accepted answer
  
  Try doubling-up your backslashes:
  
  <https://craftcms.com/docs/templating/filters#replace>
  
  > **Comments:**
  > **username_0**: Good shout, that fixed it. Any idea why double back-slashes are required?  
  > **username_1**: I don't know exactly for sure, but in general whenever you're passing one language through to another (in this case, passing a regex through twig), you need to double-escape if both languages share the same escape characters. Because the first language (twig in this case) will escape the backslash, but you want an actual backslash passed through to the regex (so it can then escape the character after it), so you need to tell twig "I want an actual backslash here to be sent to the regex". Or something like that.  
  
  ---
  
  