Skip to content

Instantly share code, notes, and snippets.

@brenapp
Created September 26, 2015 02:14
Show Gist options
  • Save brenapp/591e4da0d5b8a237e4dd to your computer and use it in GitHub Desktop.
Save brenapp/591e4da0d5b8a237e4dd to your computer and use it in GitHub Desktop.
A very basic Fuzzy Search in 66 bytes
/* Fuzzy Search in JavaScript in 66 bytes by SpeedyNinja
Creates a Regex of the Form x.*y.*z.* for query xyz and tests it against the list of terms
*/
f=t=>s=>t.filter(x=>eval("/"+s.replace(/./,"$&.*")+"/gi").test(x))
/*
Usage:
var search = f(["list", "of", "search", "terms"])
search("er") -> ["search", "terms"]
^ ^ ^^
*/
Copy link

ghost commented Sep 26, 2015

t=>s=>t.filter(x=>RegExp(s.replace(/./g,"$&.*"),"i").test(x))

i made it not use eval and also saved 3 whole bytes

i think you misplaced the g regex flag, it wasn't present in the .replace call so something like

f(["t.e.s.t"])("test")

wouldn't work, but it is there on the .test call where it's useless since it tells the regex engine to not return on the first match (pointless since .test only tests if a string matches the regex or not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment