Reputation: 2398
Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions. My regular expression doesn't account for a sentence being inside brackets and I would like to keep the delimiter.
I've put an example of the code in jsFiddle.net here
Upvotes: 1
Views: 5484
Reputation: 325
use the (?=pattern) lookahead pattern in the regex example
var string = '500x500-11*90~1+1';
string = string.replace(/(?=[$-/:-?{-~!"^_`\[\]])/gi, ",");
string = string.split(",");
this will give you the following result.
[ '500x500', '-11', '*90', '~1', '+1' ]
Can also be directly split
string = string.split(/(?=[$-/:-?{-~!"^_`\[\]])/gi);
giving the same result
[ '500x500', '-11', '*90', '~1', '+1' ]
Upvotes: 1
Reputation: 6228
@Utkanos You idea is good, but I think replace
may better:
text.replace(/\(?[A-Z][^\.]+[\.!\?]\)?/g, function (sentence) {
output += '<p>'+ sentence + '</p>';
});
You no need to loop again.
Upvotes: 1
Reputation: 34556
I took the match approach rather than split. It could be tighter (e.g. what if a sentence ends with ...
, etc).
text.match(/\(?[A-Z][^\.]+[\.!\?]\)?(\s+|$)/g);
Upvotes: 4