简体   繁体   English

用换行符匹配段落正则表达式

[英]Matching paragraph with line breaks Regex

What regex can I use to match a paragraph (including line breaks) so when I use split() , I get an array with each sentence as one element?我可以使用什么正则表达式来匹配段落(包括换行符),所以当我使用split()时,我会得到一个数组,每个句子都作为一个元素?

Something like this:像这样的东西:

const paragraph = `
  one potatoe
  two apples
  three onions
`;

const arr = paragraph.split(/(.+?\n\n|.+?$)/);

I have that regex that returns ["one potatoe↵two apples↵", "three onions", ""] but what I'm looking for is ["one potatoe", "two apples", "three onions"] .我有那个返回["one potatoe↵two apples↵", "three onions", ""]的正则表达式,但我要找的是["one potatoe", "two apples", "three onions"]

Thanks for the help!谢谢您的帮助!

EDIT :编辑

Each sentence is separated by a line break.每个句子由换行符分隔。 So after one potatoe there's a line break (hit return) and then comes two apples , line break and three onions所以在one potatoe之后有一个换行符(点击返回)然后是two apples ,换行符和three onions

I understand you want to get each line with text with as many adjacent line breaks as follow it.我了解您希望每一行都带有与后面一样多的相邻换行符的文本。

It will be easier to use match instead of split :使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM