Squirrl
Squirrl

Reputation: 4966

How can I split a body of text into both sentences and/or paragraph breaks?

I would like to split a body of text such as:

var str = "This is one.  Two because of space break
This is number three! 


And Four?!?!"

Using str.match( /[^\.!\?]+[\.!\?]+/g ) from here I get the following 3.

[ 'This is one.',
  '  Two because of space break\r\n    This is number three!',
  ' \r\n\r\n\r\n    And Four?!?!' ]

Instead I would like to have 4 different and clean (no \r\n) values because of the page break. I tried using str.replace(/\r?\n/g,'.'); before the match function and that sort of works, but I was wondering if there was a cleaner way maybe by combining regexes?

I would like to get:

['This is one.', 'Two because of space break', 'This is number three!', 'And Four?!?!']

Upvotes: 0

Views: 687

Answers (1)

jcaron
jcaron

Reputation: 17710

Is this what you want?

str.match(/[^\s.!?]+[^.!?\r\n]+[.!?]*/g);

Upvotes: 2

Related Questions