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
\n. Matches a newline character. \r. Matches a carriage return character.
Definition and Usage The \r metacharacter matches carriage return characters.
If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.
Try this. text = text . Replace("\\r\\n", "");
I think
str.gsub(/\r?\n/, '<br>')
should do the job
\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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With