Search code examples
javascriptarraysstringstring-matching

what is best way to match words from list to words from sentence in javascript?


i have two sentences and i would like to find all the words they share regardless of capitalization or punctuation. currently this is what I am doing:

    searchWords = sentence1.split(" ");
    var wordList = sentence2.split(" ");
    const matchList = wordList.filter(value => -1 !== searchWords.indexOf(value));

it works ok but obviously capitalization and punctuation cause issues. i know i need to incorporate something like .match() in there but i don't know how to work with it. I am sure this is something someone has done before just havent found the code yet, any refrences are also appreciated.

Thank you,

Best

This dude.


Solution

  • If you're looking for any words that match you can use RegExp with String.prototype.replace and verify a match using String.prototype.search with a created RegExp and an i flag to allow case insensitivity.

    function compare(str1, str2, matches = []) {
         str1.replace(/(\w+)/g, m => str2.search(new RegExp(m, "i")) >= 0 && matches.push(m));
         return matches;
     }
     
     console.log( compare("Hello there this is a test", "Hello Test this is a world") );


    If you're looking for specific words that match you can use functional composition to split each string into an Array, filter each by possible matches, and then filter one against the other.

    function compare(str1, str2, matchables) {
         let containFilter = (a) => (i) => a.includes(i),
         matchFilter = s => s.toLowerCase().split(" ").filter(containFilter(matchables));
         
        return matchFilter(str1).filter(containFilter( matchFilter(str2) ));
     }
     
     let matchables = ["hello", "test", "world"];
     console.log( compare("Hello there this is a test", "Hi Test this is a world", matchables) );