<p>Just needed something similar today, and patched in a new replace filter to the <a href="https://github.com/mmikkel/RetconHTML-Craft" rel="nofollow">RetconHTML plugin</a>. The filter – called <code>retconReplace</code> – uses <code>preg_replace</code> and might be able to help you out:</p>

<pre><code>{{ string|retconReplace('/\s*$/m') }}
</code></pre>

<p>You'll find the wiki on <code>retconReplace</code> <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Replace" rel="nofollow">here</a>.</p>

CC BY-SA 3.0
2015-10-16T15:25:54.427Z
That’s awesome, thanks Mats. And your plugin has come in very handy on another site I've been working on :)
Katrin
CC BY-SA 3.0
2015-09-30T13:35:05.920Z
2015-09-30T13:35:05.920Z
Mats Mikkel Rummelhoff
<p>Would the <code>spaceless</code> <a href="http://twig.sensiolabs.org/doc/tags/spaceless.html" rel="nofollow">filter</a> work wrapped in div?</p>

CC BY-SA 3.0
2015-09-24T14:45:11.983Z
If you run the field value through `|spaceless` (with or without a div), it will only remove the whitespace at the outer edges of the text; not all of the trailing whitespace on each line of the text.
Brandon Kelly
CC BY-SA 3.0
2015-09-24T12:14:17.167Z
2015-09-24T12:14:17.167Z
Steven Grant
<p>Currently it’s not possible to pass regex modifiers to the <code>|replace</code> filter. You’d have to just do this manually:</p>

<pre><code>{% set str = "foo     \nbar    " %}
{% set trimmed = "" %}

{% for line in str|split("\n") %}
    {% set trimmed = trimmed ~ (trimmed ? "\n") ~ line|replace('/\\s*$/', '') %}
{% endfor %}
</code></pre>

<p><strong>UPDATE</strong></p>

<p>As of Craft 2.5, the <code>|replace</code> filter supports regex modifiers.</p>

CC BY-SA 3.0
2015-09-24T14:38:21.437Z
2015-12-05T15:15:49.863Z
2015-12-05T15:15:49.863Z
Brandon Kelly
<p>I have a string from a multi-line plain text field and I'd like to trim any trailing spaces before new lines.</p>

<p>With plain regex I would use this expression: <code>/\s*$/gm</code></p>

<p>Using</p>

<pre><code>{{ string|replace('/\\s*$/', '') }}
</code></pre>

<p>will trim out the last trailing space of the string (same as the <code>trim</code> filter would), but not before every new line.</p>

<p>I have tried</p>

<pre><code>{{ string|replace('/\\s*$/gm', '') }}
</code></pre>

<p>and</p>

<pre><code>{{ string|replace('/\\s*$/m', '') }}
</code></pre>

<p>but that doesn't have any effect.</p>

<p>How can I apply the <code>m modifier</code> to the replace filter?</p>

<filter><replace>
Trim trailing spaces before each new line in a multi-line string
# Trim trailing spaces before each new line in a multi-line string

(Asked by: username_0 on 2015-09-15)

I have a string from a multi-line plain text field and I'd like to trim any trailing spaces before new lines.
With plain regex I would use this expression: /\s*$/gm
Using
{{ string|replace('/\\s*$/', '') }}

will trim out the last trailing space of the string (same as the trim filter would), but not before every new line.
I have tried
{{ string|replace('/\\s*$/gm', '') }}

and
{{ string|replace('/\\s*$/m', '') }}

but that doesn't have any effect.
How can I apply the m modifier to the replace filter?

**Tags:** filter, replace


---

## 3 answers

### Answer by username_3 on 2015-09-24. Score: 4 | Accepted answer

Currently it’s not possible to pass regex modifiers to the |replace filter. You’d have to just do this manually:
{% set str = "foo     \nbar    " %}
{% set trimmed = "" %}

{% for line in str|split("\n") %}
    {% set trimmed = trimmed ~ (trimmed ? "\n") ~ line|replace('/\\s*$/', '') %}
{% endfor %}

UPDATE
As of Craft 2.5, the |replace filter supports regex modifiers.


---

### Answer by username_1 on 2015-09-30. Score: 1

Just needed something similar today, and patched in a new replace filter to the RetconHTML plugin. The filter – called retconReplace – uses preg_replace and might be able to help you out:
{{ string|retconReplace('/\s*$/m') }}

You'll find the wiki on retconReplace here.

> **Comments:**
> **username_0**: That’s awesome, thanks Mats. And your plugin has come in very handy on another site I've been working on :)  

---

### Answer by username_2 on 2015-09-24. Score: 0

Would the `spaceless` [filter](http://twig.sensiolabs.org/doc/tags/spaceless.html) work wrapped in div?

> **Comments:**
> **username_3**: If you run the field value through `|spaceless` (with or without a div), it will only remove the whitespace at the outer edges of the text; not all of the trailing whitespace on each line of the text.  

---

