Split on different newlines

前端 未结 8 1095
自闭症患者
自闭症患者 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条回答
  •  臣服心动
    2021-02-01 13:00

    \n is for unix 
    \r is for mac 
    \r\n is for windows format
    

    To be safe for operating systems. I would do /\r?\n|\r\n?/

    "1\r2\n3\r\n4\n\n5\r\r6\r\n\r\n7".split(/\r?\n|\r\n?/)
    => ["1", "2", "3", "4", "", "5", "", "6", "", "7"]
    

提交回复
热议问题