Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get sentence between dots and replace it

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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("."))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading