') This works gre...">

Regular expression for strip \ r and \ n or \ r \ n

I use the following regex to create newline tags:

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

This works great on the desktop. there is only \ n on the text of the iphone, it does not have \ r.

How can I create regex support? \ r \ n or just \ n?

thanks

+6
source share
5 answers

\r\n|\r|\n

This regex will also allow you to support Macs that only use \r as the end of a line. Since regular expressions are greedy, they will match \r\n , and not separate ones, if possible.

+8
source

I think,

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

must do the job

+12
source

operator ? accepts 0 or one of the previous element, so \r?\n should accept \r or \n or \r\n

+1
source

This is probably the easiest way:

 str.gsub(/\R/, '<br>') 

/\R/ works for Mac / Linux / Windows and includes more exotic unicode strings.

+1
source

\r?\n|\r

\r?\n will match \n or \r\n .

\r will only match \r .

This should work for Windows \ Mac \ Linux EOL.

0
source

Source: https://habr.com/ru/post/907458/


All Articles