Fellow Stranger
Fellow Stranger

Reputation: 34013

Replace 1 newline with <br>, >= 2 with <br><br>

I use the following to replace every linebreak with a <br>:

h(my_string).gsub(/(?:\n\r?|\r\n?)/, '<br>').html_safe

I wonder if there would be any solution where if there's a single linebreak, then replace it with a single <br>. And if there are two or more linebreaks, then replace all with only two <br>, i.e. <br><br>?

Upvotes: 0

Views: 72

Answers (1)

Nikita Misharin
Nikita Misharin

Reputation: 2020

You can iterate couple of times

h(my_string).gsub(/(\n\r?){2,}/, '<br><br>').gsub(/\n/, '<br>').html_safe

Upvotes: 2

Related Questions