Created
October 15, 2012 15:49
-
-
Save wayneashleyberry/3893207 to your computer and use it in GitHub Desktop.
fuzzy string matching in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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