首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript根据行数将存储在字符串中的长文件拆分为字符串数组

Javascript根据行数将存储在字符串中的长文件拆分为字符串数组
EN

Stack Overflow用户
提问于 2020-06-18 14:50:42
回答 1查看 27关注 0票数 0

我已经寻找了其他解决方案,但它们都不符合我的要求。

假设我有一个很长的文件,在var str中包含以下数据

代码语言:javascript
复制
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

我希望它被分解成一个数组的3个字符串的基础上的新行。所以新数组,比如var strSplitstrSplit[0]

代码语言:javascript
复制
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

我希望str根据每段文本后的空行进行拆分。我想不通了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-18 15:04:40

你可以用str.split(/\r?\n\r?\n/); split你的字符串。请在下面查看它。

代码语言:javascript
复制
var str = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.`;

var result = str.split(/\r?\n\r?\n/);
console.log(result.length)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62443992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档