在不同的换行符上拆分
问题描述:
现在我正在对字符串执行split
,并假设来自用户的换行符是\r\n
,如下所示:
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/)
我想在\r\n
或\n
上拆分.
那么正则表达式将如何拆分其中的两个?
So how what would the regex be to split on either of those?
答
您尝试过/\r?\n/
吗? ?
使\r
为可选.
Did you try /\r?\n/
? The ?
makes the \r
optional.