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);
One way to achieve fuzzy matches is using
somefunction with a custom predicate to check for partial matches. Something like:Footnote: In case you want to do it without using
lodash, you can replace_.somewithArray.protoype.some