Reputation: 1555
I am trying to find the sentence that contains a specific word.
I defined a sentence starting and ending with the following characters :. ! ?
var str = "Hello, how is it going. This is the bus we have to take!";
var regex = /[^.?!]*(?:[.?,\s!])(bus)(?=[\s.?!,])[^.?!]*[.?!]/igm;
var result = regex.exec(str);
output : `This is the bus we have to take!`
Now, I have trouble when I try to find the sentence that contains the word hello
, as it's starting the sentence. How could I change my regex to include that case? I am not used to regex and it's quite hard to get into it, even with the docs under my eyes!
Upvotes: 2
Views: 3184
Reputation: 626903
Remember that splitting text into linguistic sentences is a very specific, difficult task usually performed with the help of NLP packages.
If you want to limit to specific strings that follow your definition of a sentence:
/[.?!]/
regexRegExp#test()
since you need a case insensitive checkvar str = "Hello, how is it going. This is the bus we have to take!";
var chunks = str.split(/[.?!]/).filter(function(n) {
return /hello/i.test(n);
});
console.log(chunks);
Note that to check for a whole word you may use /\bhello\b/i
or /(?:^|\s)hello(?!\S)/i
regexps depending on further requirements.
Upvotes: 3