AnApprentice AnApprentice - 7 months ago 16
Ruby Question

Regex to strip \r and \n or \r\n

I use the following regex to make newlines
tags:

str.gsub("\r\n", '<br>')


This works fine on a desktop. on an iphone text only has \n, it doesn't have the \r.

How can I make the regex support either? \r\n or just \n ?

Thanks

Answer

\r\n|\r|\n

That regular expression will also let you support Macs, which use \r alone as a line ending. Since regexes are greedy, they will match the \r\n as opposed to the individual ones if possible.