Split on different newlines

前端 未结 8 1099
自闭症患者
自闭症患者 2021-02-01 12:57

Right now I\'m doing a split on a string and assuming that the newline from the user is \\r\\n like so:

string.split(/\\r\\n/)
<         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 13:25

    The alternation operator in Ruby Regexp is the same as in standard regular expressions: |

    So, the obvious solution would be

    /\r\n|\n/
    

    which is the same as

    /\r?\n/
    

    i.e. an optional \r followed by a mandatory \n.

提交回复
热议问题