[英]How to split string by empty line in javascript
I have string loaded from file: 我从文件中加载了字符串:
var data = load("file.txt");
variable data
is: 可变
data
为:
1
2
3
a
b
c
d
e
How to split this variable into two arrays like this: 怎样将这个变量分成两个数组:
[1, 2, 3]
and 和
[a, b, c, d, e]
I try data.split("\\n");
我尝试
data.split("\\n");
and data.split("\\r\\n");
和
data.split("\\r\\n");
but it doesn`t work. 但这不起作用。
Thank for help. 感谢您的帮助。
Try with this: 试试这个:
var str = `1 2 3 a b c d e` var splitted = str.split(/\\n\\s*\\n/) splitted.forEach((capture, i) => console.log(`Capture #${i}:\\n${capture}`));
Since the space represents two line breaks you can try something like this: 由于空格代表两个换行符,因此您可以尝试执行以下操作:
var data = originalData.split("\n\n");
Then one line break: 然后换行:
data.forEach(function(data, index){ data[index] = data.split("\\n"); })
; data.forEach(function(data, index){ data[index] = data.split("\\n"); })
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.