如何在JavaScript中用空行分割字符串 [英] How to split string by empty line in javascript

查看:169
本文介绍了如何在JavaScript中用空行分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文件中加载了字符串:

I have string loaded from file:

var data = load("file.txt");

变量 data 是:

1
2
3

a
b
c
d
e

如何将此变量分成两个数组:

How to split this variable into two arrays like this:

[1, 2, 3]

[a, b, c, d, e]

我尝试 data.split("\ n"); data.split("\ r \ n"); 无效,

感谢帮助.

推荐答案

由于空格代表两个换行符,因此您可以尝试以下操作:

Since the space represents two line breaks you can try something like this:

var data = originalData.split("\n\n");

然后一个换行符:

data.forEach(function(data,index){data [index] = data.split("\ n");});

这篇关于如何在JavaScript中用空行分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆