Home > Blockchain >  Get sentence between dots and replace it
Get sentence between dots and replace it

Time:08-29

I need some help with regular expression. I have a text message, and I should find two or more matches (words) in this text and replace (remove) all sentence. For example:

Words: pen, apple, juice

Text: I like juice. I like juice and apple. I like it so much. It`s my pen.

Output should be: I like juice. I like it so much. It`s my pen.

I think I need to separate text into sentences and find two or more matches, but I don`t know how I can do it.

CodePudding user response:

Explanation. We first split by . to get sentences array. Then we filter it by counting the filtered words array against sentence counting existence and hoping it would be less than 2. Basically that's it. Then we join by . back.

var words = ['pen', 'apple', 'juice'];

var text = "I like juice. I like juice and apple. I like it so much. It`s my pen."

var arr = text.split(".");

console.log(arr.filter(sentence => words.filter(word => sentence.indexOf(word)>-1).length<=1).join("."))

  • Related