krakig
krakig

Reputation: 1555

Get sentence containing specific word with regex

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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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:

  • Split with /[.?!]/ regex
  • Check if the entry contains a substring with RegExp#test() since you need a case insensitive check

var 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

Related Questions