var substringMatcher = function(strs) { return function findMatches(q, cb) { var matches, substringRegex; // an array that will be populated with substring matches matches = []; // regex used to determine if a string contains the substring `q` substrRegex = new RegExp(q, 'i'); // iterate through the pool of strings and for any string that // contains the substring `q`, add it to the `matches` array $.each(strs, function(i, str) { if (substrRegex.test(str)) { matches.push(str); } }); cb(matches); }; }; var products = ['Cavalier King Charles Spaniel Chestnut Red Male','Dog Name 11','Dog Name 12','Dog Name 23','Dog Name 34','Dog Name 35','Dog Name 39','Dog Name 4','Dog Name 40','Dog Name 41','Dog Name 43','Dog Name 44','Dog Name 45','Dog Name 48','Dog Name 52','Dog Name 53','Dog Name 54','Dog Name 9','Dog Name: Benji','Dog Name: Cornelius','Dog Name: Jack','Puppy Name 22','Puppy Name 42','Puppy Name 46','Stud Dog' ]; var productsIDs = [ ]; $('#our-products .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'products', limit: 40, source: substringMatcher(products) });