Reputation: 110960
Given the following text in a textarea:
Line 1 stuff.. Line 1 stuff.. Line 1 stuff.. Line 1 stuff.. Line 1 stuff..
Line 2 stuff.. Line 2 stuff.. Line 2 stuff.. Line 2 stuff.. Line 2 stuff..
I want to convert the new lines to <BR>
tags, not use the simple_format <P>
tags...
So I tried:
str = str.gsub("\r\n", '<br>')
Problem is this is making two <BR>
tags:
<div class="message">line 1<br><br>Line 2</div>
How can make just one <BR>
tag?
Upvotes: 3
Views: 4932
Reputation:
Best way to do this is to use simple_format(str)
:
simple_format()
is a helper function which will convert new lines and carriage returns to<br>
and<p>...</p>
tags.
reference: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Upvotes: 7
Reputation: 30445
str = str.gsub(/[\r\n]+/, "<br>")
This will turn any number of consecutive \r
and/or \n
characters into a single <br>
.
Upvotes: 6
Reputation: 1892
This would mean that you have two occurrences of "\r\n". So you could sanitize the input or expect this situation in your regex.
Have a look at this: Ruby gsub / regex modifiers?
Upvotes: 1
Reputation: 539
You have two line breaks in the input text. If you want there to be one <br/>
, just do:
str = str.gsub("\r\n\r\n", '<br />');
Upvotes: 0