Reputation: 13
I've created a regular expression for JavaScript that's supposed to "some what smartly" get the first sentence from a string of text/sentences. I used RegExr to create and test it:
However, when I actually implement that regular expression:
/.*(.)(?=\s[A-Z])/g
In my JavaScript code I'm using split and I'm getting everything but the first sentence. Here's the output:
[ '', '.', ' Saturday, troopers got a report that […]' ]
If any RegEx masters see the error in my ways, and/or have any tips or solutions, I'd be very grateful.
Thanks!
PS: Yes, I've already searched for this...
Upvotes: 1
Views: 1664
Reputation: 18950
Try it like this:
const regex = /.*?(\.)(?=\s[A-Z])/;
const str = `© 2018 Telegraph Publishing LLC Four area men between the ages of 18 and 20 were arrested early this morning on a variety of charges in an overnight burglary at the Tater Hill Golf Course in Windham. According to a Vermont State Police press release, at about 2:30 a.m. Saturday, troopers got a report that […]`;
let m;
if ((m = regex.exec(str)) !== null) {
console.log(m[0]);
}
I use a lazy regex match without the g
flag to only retrieve the first result.
Or go with a negated character class as it is more efficient: /^[^.]+\.(?= [A-Z])/
Upvotes: 1