AnApprentice AnApprentice - 2 years ago 114
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 ?


Answer Source


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download