Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Created October 15, 2012 15:49
Show Gist options
  • Save wayneashleyberry/3893207 to your computer and use it in GitHub Desktop.
Save wayneashleyberry/3893207 to your computer and use it in GitHub Desktop.
fuzzy string matching in javascript
var people = ['Daniel', 'Dustin', 'David', 'Damarcus', 'Russ'];
function matchPeople(input) {
var regex = new RegExp('[' + input.split('').join(']+.*[') + ']+', 'ig');
return people.filter(function(person) {
if (person.match(regex)) return person;
});
}
console.log(matchPeople('dvd')); // returns ['David']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment