Sebastian Olsen Sebastian Olsen - 5 months ago 22
Javascript Question

How to match paragraphs using regex in javascript?

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

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


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):

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?


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

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.