Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-16T15:25:54.427Z
Answers.Text: That’s awesome, thanks Mats. And your plugin has come in very handy on another site I've been working on :)
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-30T13:35:05.920Z
Answers.LastActivityDate: 2015-09-30T13:35:05.920Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-24T14:45:11.983Z
Answers.Text: 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.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-24T12:14:17.167Z
Answers.LastActivityDate: 2015-09-24T12:14:17.167Z
Answers.UserDisplayName: Steven Grant
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-24T14:38:21.437Z
Answers.LastActivityDate: 2015-12-05T15:15:49.863Z
Answers.LastEditDate: 2015-12-05T15:15:49.863Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <filter><replace>
Title: Trim trailing spaces before each new line in a multi-line string
ThreadText: # 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](https://github.com/mmikkel/RetconHTML-Craft). 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](https://github.com/mmikkel/RetconHTML-Craft/wiki/Replace).
  
  > **Comments:**
  > **username_0**: That’s awesome, thanks username_1. 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.  
  
  ---
  
  