-4

I have a list of strings such as

const myCategories = ["Alligator", "Rhyno", "Elephant"]

And I want to get the best match for the string which I provide as input, for example

const userInput = "elep"

return myCategories.find(category => category.test(userInput))

However this does not work as I expect.

I need this to return a category even with a partial match, for example, if i write "eph", I should get "Elephant", or "rho" I should get "Rhyno"

  • You can use [something like this](https://stackoverflow.com/questions/10473745/compare-strings-javascript-return-of-likely) to get the 'score' of each word, then show the one with the highest. – 0stone0 Oct 25 '21 at 14:19

1 Answers1

1

What it sounds like you're looking for is not regex, but a fuzzy string comparison. Try Javascript fuzzy search that makes sense for more information.

Trenton Telge
  • 478
  • 3
  • 17