Sebastian Olsen Sebastian Olsen - 1 month ago 10
Javascript Question

Match paragraph breaks, but not line breaks

I'd like a regex that matches paragraphs, so for example :


The red brown fox, did something.
[newline] I don't remember this text.

[newline]

[newline]
So, instead I'll say blah blah.
[newline] Blah.


would return an array like this:

['The red brown...', 'So instead I'll say...']


I already have this regex (that I stole, shh):
/(?:[^\r\n]|\r(?!\n))+/gm


However this pattern matches both linebreaks (one newline) and paragraphs breaks (two newline). How can I match the body of text between paragraphs, but not split the matches between single linebreaks?

Answer

You may split with the linebreak (sequence, depends on the linebreak style) with a {2,} limiting quantifier:

var s = "The red brown fox, did something.\r\nI don't remember this text.\r\n\r\nSo, instead I'll say blah blah.\r\nBlah.";    
console.log(s.split(/(?:\r\n){2,}/));

So, here, /(?:\r\n){2,}/ matches 2 or more consecutive CR+LF sequences. If the linebreak style is LF only, use a simpler /\n{2,}/ pattern.

Comments