Search code examples
javascriptarrayslodash

Get partial string match of string Array using Lodash


I have an array of strings and want to find partial string matches using Lodash. I'd like the search to find full matches (such as with broken) but also partial matches (such as with virus being part of viruses)

const textToSearch = 'The broken ship that caught fire also had viruses onboard';
const arrayOfKeywords = ['virus', 'fire', 'broken'];

So far I have a method that will split the textToSearch into an array and then find full matches using _.include()

const tokenize = text =>
    text
      .split(/(\w+)/)
      .map(x =>
        _.includes(arrayOfKeywords, x.toLowerCase())
          ? {content: x, type: 'keyword'}
          : x
      );

const tokens = tokenize(textToSearch);

Solution

  • One way to achieve fuzzy matches is using some function with a custom predicate to check for partial matches. Something like:

    const textToSearch = 'The broken ship that caught fire also had viruses onboard';
    const arrayOfKeywords = ['virus', 'fire', 'broken'];
    
    const tokenize = text =>
      text
        .split(/(\w+)/)
        .map((x) =>
          _.some(arrayOfKeywords, (keyword) => x.toLowerCase().includes(keyword.toLowerCase()))
            ? { content: x, type: 'keyword' }
            : x
        );
    
    const tokens = tokenize(textToSearch);
    console.log(tokens);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>


    Footnote: In case you want to do it without using lodash, you can replace _.some with Array.protoype.some