[英]Matching paragraph with line breaks Regex
我可以使用什么正则表达式来匹配段落(包括换行符),所以当我使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.