Answers.Body: <p><strong>UPDATE:</strong> <a href="http://buildwithcraft.com/updates#build2579" rel="nofollow noreferrer">As of Craft v2.2</a>, the replace filter now supports regex:</p>
  
  <blockquote>
    <p>The replace filter now supports regular expressions, by setting the first argument to a string that begins and ends with forward slashes, e.g. '/foo/'.</p>
  </blockquote>
  
  <p>So this should work by adding forward slashes.</p>
  
  <pre><code>{% set bar = '/%20AND%20foo%3A%22(.+?)%22/' %}
  </code></pre>
  
  <p>I think this is particular to Craft and not native to the Twig replace filter.</p>
  
  <hr>
  
  <p><del>I don't think the <a href="http://twig.sensiolabs.org/doc/filters/replace.html" rel="nofollow noreferrer">Twig replace filter</a> will allow for regex matches.</del></p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/228/how-can-i-perform-a-regex-match-in-twig">According to this post</a>, Twig only uses regular expressions with Comparisons.</p>
  
  <p>But there are third-party plugins which might work:</p>
  
  <ul>
  <li><a href="https://github.com/victor-in/Craft-TwigPCRE" rel="nofollow noreferrer">Twig PCRE Filters</a> </li>
  <li><a href="https://github.com/low/low_regex/" rel="nofollow noreferrer">Low Regex</a></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-29T12:16:39.737Z
Answers.Text: As pointed out by Low, [Craft's replace filter](http://buildwithcraft.com/docs/templating/filters#replace) does now support regex.
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-19T12:49:10.857Z
Answers.Text: Got it sorted with Low Regex. Thanks for the hint! I was somehow under the impression that the |replace filter can do this.
Answers.UserDisplayName: Florian
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-19T12:14:10.240Z
Answers.LastActivityDate: 2015-08-03T21:23:34.083Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Alex Roper
Body: <p>any idea why this: <code>%20AND%20foo%3A%22(.+?)%22</code> won't match this: <code>%20AND%20foo%3A%22some random string of characters%22</code> in the Twig <code>|replace</code> filter?</p>
  
  <p>If i do this:</p>
  
  <pre><code>{% set foo = '%20AND%20foo%3A%22some random string of characters%22' %}
  {% set bar = '%20AND%20foo%3A%22(.+?)%22' %}
  {{ foo |replace(bar, 'replaced') }}
  {# OR THIS #}
  {{ foo |replace((bar), 'replaced') }}
  </code></pre>
  
  <p>The result is <code>foo</code>. Nothing gets replaced. I'd expect the result to be the string "replaced". What am I doing wrong?</p>
  
Tags: <regex><replace>
Title: Use regex in replace filter
ThreadText: # Use regex in replace filter
  
  (Asked by: username_0 on 2014-12-19)
  
  any idea why this: `%20AND%20foo%3A%22(.+?)%22` won't match this: `%20AND%20foo%3A%22some random string of characters%22` in the Twig `|replace` filter?
  
  If i do this:
  
  ```
  {% set foo = '%20AND%20foo%3A%22some random string of characters%22' %}
  {% set bar = '%20AND%20foo%3A%22(.+?)%22' %}
  {{ foo |replace(bar, 'replaced') }}
  {# OR THIS #}
  {{ foo |replace((bar), 'replaced') }}
  
  ```
  
  The result is `foo`. Nothing gets replaced. I'd expect the result to be the string "replaced". What am I doing wrong?
  
  **Tags:** regex, replace
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-12-19. Score: 3 | Accepted answer
  
  **UPDATE:** [As of Craft v2.2](http://buildwithcraft.com/updates#build2579), the replace filter now supports regex:
  
  > The replace filter now supports regular expressions, by setting the first argument to a string that begins and ends with forward slashes, e.g. '/foo/'.
  
  So this should work by adding forward slashes.
  
  ```
  {% set bar = '/%20AND%20foo%3A%22(.+?)%22/' %}
  
  ```
  
  I think this is particular to Craft and not native to the Twig replace filter.
  
  ---
  
  ~~I don't think the [Twig replace filter](http://twig.sensiolabs.org/doc/filters/replace.html) will allow for regex matches.~~
  
  [According to this post](https://craftcms.stackexchange.com/questions/228/how-can-i-perform-a-regex-match-in-twig), Twig only uses regular expressions with Comparisons.
  
  But there are third-party plugins which might work:
  
  * [Twig PCRE Filters](https://github.com/victor-in/Craft-TwigPCRE)
  * [Low Regex](https://github.com/low/low_regex/)
  
  > **Comments:**
  > **username_0**: Got it sorted with Low Regex. Thanks for the hint! I was somehow under the impression that the |replace filter can do this.  
  > **username_2**: As pointed out by Low, [Craft's replace filter](http://buildwithcraft.com/docs/templating/filters#replace) does now support regex.  
  
  ---
  
  