amoeboar
amoeboar

Reputation: 355

JavaScript regex, start sentence on capital letter only in between periods

I'm trying to get my regex to return the right substring of a paragraph text. I've determined that the best thing to do would be to pull in any sentence which comes between two periods, and which begins with an uppercase letter only (no numbers or punctuation).

What I have so far returns every other sentence between two periods (not sequential sentences), and also I can't figure out how to grab a sentence that only begins with a capital letter.

Here's the expression:

var bodyStr = result.body;
var truncatedStr = bodyStr.match( /[^\.!\?]+[\.!\?]+/g );

I'm using regex101 to try and make the second part of this work (pulling sentences that begin with capital, non-number, non punctuation characters) and it breaks:

https://regex101.com/r/rJ8sC5/1

Can anyone help?

Upvotes: 2

Views: 1142

Answers (1)

Pavel Gatnar
Pavel Gatnar

Reputation: 4053

Just add the capital letter to your regex:

/[A-Z][^\.!\?]*[\.!\?]+/g

Upvotes: 3

Related Questions