<p>Craft's replace filter only supports doing multiple strings replacements at once.  If you want to do a regex replacement, it will have to be it's own separate call.</p>

<p>In your case, you'd have to do something like this:</p>

<pre><code>{% set url =  "http://www.google.com/"|replace('http://', '') %}
{{ url|replace('/\\/$/', '') }}
</code></pre>

CC BY-SA 3.0
2015-05-17T19:47:31.040Z
2015-05-17T19:47:31.040Z
Brad Bell
<p>I think you are getting the twig <a href="http://twig.sensiolabs.org/doc/filters/replace.html" rel="noreferrer">replace</a> filter (which does not have the regex capability) instead of the Craft <a href="http://buildwithcraft.com/docs/templating/filters#replace" rel="noreferrer">replace</a> filter (which does).</p>

<p>So this is a bug in Craft, in that their filter is masked by the twig one.</p>

<p>You could use the <a href="http://twig.sensiolabs.org/doc/filters/trim.html" rel="noreferrer">trim</a> filter to remove trailing <code>/</code>s:</p>

<pre><code>website|replace({'http://':''})|trim('/')
</code></pre>

<p>Or you could use one of Victor's <a href="https://github.com/victor-in/Craft-TwigPCRE/" rel="noreferrer">Twig PCRE Filters</a>.</p>

CC BY-SA 3.0
2015-04-29T15:20:37.977Z
Thank you Marion! Completely forgot about the `trim` filter - perfect solution :)
Katrin
CC BY-SA 3.0
2015-05-17T19:49:51.220Z
Craft's replace filter overrides Twig's, not visa-versa - although we do keep their base behavior in tact in ours.  I've added an answer for the behavior you were seeing.
Brad Bell
CC BY-SA 3.0
2015-04-29T14:47:01.757Z
2015-04-29T14:47:01.757Z
Marion Newlevant
<p>I am trying to remove the <code>http://</code> and trailing slash from a string, so my replace filter looks like this:</p>

<p><code>website|replace({ 'http://': '', '/\\/$/': '' })</code></p>

<p>The <code>http://</code> part works, the trailing slash one doesn't. Why not?</p>

<regex><replace>
Regex to remove trailing slash not working
# Regex to remove trailing slash not working

(Asked by: username_0 on 2015-04-29)

I am trying to remove the `http://` and trailing slash from a string, so my replace filter looks like this:

`website|replace({ 'http://': '', '/\\/$/': '' })`

The `http://` part works, the trailing slash one doesn't. Why not?

**Tags:** regex, replace


---

## 2 answers

### Answer by username_2 on 2015-04-29. Score: 7 | Accepted answer

I think you are getting the twig replace filter (which does not have the regex capability) instead of the Craft replace filter (which does).
So this is a bug in Craft, in that their filter is masked by the twig one.
You could use the trim filter to remove trailing /s:
website|replace({'http://':''})|trim('/')

Or you could use one of Victor's Twig PCRE Filters.

> **Comments:**
> **username_0**: Thank you Marion! Completely forgot about the `trim` filter - perfect solution :)  
> **username_1**: Craft's replace filter overrides Twig's, not visa-versa - although we do keep their base behavior in tact in ours.  I've added an answer for the behavior you were seeing.  

---

### Answer by username_1 on 2015-05-17. Score: 2

Craft's replace filter only supports doing multiple strings replacements at once.  If you want to do a regex replacement, it will have to be it's own separate call.
In your case, you'd have to do something like this:
{% set url =  "http://www.google.com/"|replace('http://', '') %}
{{ url|replace('/\\/$/', '') }}


---

