nataliya22k
nataliya22k

Reputation: 13

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?

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"].

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

Upvotes: 1

Views: 1001

Answers (1)

trincot
trincot

Reputation: 350147

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(/^.+$[\n\r]*/gm);

console.log(arr);

Upvotes: 3

Related Questions