AnApprentice AnApprentice - 6 months ago 3x
Ruby Question

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

I use the following regex to make newlines

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 ?




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.