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 = ['£10 Gift Voucher','Bakewell Cake','Banana Loaf','Butterscotch & Cranberry Flapjacks','Carrot Cake','Cherry & Almond Genoa Loaf','Cherry Bakewell Traycake','Chocolate & Orange Cake','Chocolate & Raspberry Cake','Chocolate Brownie Traycake','Chocolate Sponge Traycake','Cockburns Fruit Tea Loaf','Coconut, Almond & Cherry Cake','Coffee & Walnut Cake','Courgette & Lime Cake','Cupcakes','Lemon & Blackcurrant Cake','Lemon & Poppyseed Cake','Lemon Drizzle Loaf','Madeira Loaf','Orange & Lavender Cake','Plum & Ginger Cake','Rich Fruit Cake','Rose & Raspberry Cake','Sparkling Afternoon Tea for Two','Sussex Apple Cake','Sussex Craggy Roosters','Sussex Cream Tea For Two','Traditional Afternoon Tea For Two','Victoria Sponge Cake' ]; $('#our-products .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'products', limit: 20, source: substringMatcher(products) });