I am currently using the Javascript Search() method to search for a match in a string. I want to find the closest match, for example, if the string is 'apple iphone', and a person searches 'iphone 6' I want there to be a match, but for some reason, the .search method doesn't work this way.
Is there a more efficient way to use this or another method that can be more useful?
const test = "apple iphone" // this doesn't return -1 indicating there was a match console.log(test.search('iphone')) // this returns -1 indicating there wasn't any match at all despite there // being the word "iphone" in there console.log(test.search('iphone 5'))
.splitfunction. So your string will be split by space indicator and then you can useindexOfonce foriphone 5, next time foriphoneand lastly for5iphone 5or nuh then you would execute the reduce function.test.search(term.split(/\s/).join('|')).