javascript - 用换行符匹配段落正则表达式
问题描述
我可以使用什么正则表达式来匹配段落(包括换行符),所以当我使用 时split()
,我会得到一个数组,其中每个句子作为一个元素?
像这样的东西:
const paragraph = `
one potatoe
two apples
three onions
`;
const arr = paragraph.split(/(.+?\n\n|.+?$)/);
我有那个返回的正则表达式,["one potatoe↵two apples↵", "three onions", ""]
但我正在寻找的是["one potatoe", "two apples", "three onions"]
.
谢谢您的帮助!
编辑:
每个句子由换行符分隔。所以在one potatoe
有一个换行符(点击返回)然后来了two apples
,换行符和three onions
解决方案
我了解您希望每一行都带有与后面一样多的相邻换行符的文本。
它会更容易使用match
,而不是split
:
const paragraph = `
one potatoe
two apples
three onions`;
const arr = paragraph.match(/^.+$[\n\r]*/gm);
console.log(arr);
推荐阅读
- javascript - 需要帮助格式化 Phonegap/Cordova 应用程序的小时、分钟和秒。在 Android 上工作,但不是在 ios 上工作 .. 让 Nan
- python - 如何查看 Dash 应用中的活跃用户数?
- javascript - 没有重复和固定长度的数组的排列
- javascript - 有没有办法隐藏您最小化浏览器或从某个窗口失去焦点的事实
- mysql - 查找从上一个日历月开始增加流式传输时间的用户
- java - Intellij IDE 在实体中显示错误突出显示
- android - 如何获取 android OAuth 的令牌?
- eclipse - Eclipse 2020-03 上 SCALA-Ide 插件的性能调优
- php - PHP 锦标赛系统 Team vs Team
- object - 为什么不为类中创建的静态对象调用构造函数?