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?
Something like this:
const paragraph = ` one potatoe two apples three onions `; const arr = paragraph.split(/(.+?nn|.+?$)/);
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"]
.
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
Advertisement
Answer
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
:
const paragraph = ` one potatoe two apples three onions`; const arr = paragraph.match(/^.+$[nr]*/gm); console.log(arr);