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','Assam Leaf Tea','Bread Pudding','Bueno Cookie','Chai Tea Loaf','Chocolate Caramel Brownie (sold out)','Chocolate Flake Cookie','Chocolate Orange Brownie','Chocolate Orange Cookie','Cookies & Cream Brownie','Cookies & Cream Rocky Road','Creme Egg Rocky Road (Sold out)','Earl Grey Leaf Tea','Empire Biscuit','English Breakfast Leaf Tea','Kinder Chocolate Cookie','Lemon Drizzle Traycake','Lotus Biscoff Cookie','Lotus Biscoff Gold Bar Tiffin','Mars Bar Cookie','Masala Chai Leaf Tea','Milky Bar & Strawberry Cookie','Milky Way Millionaires Brownie','Millionaires Shortbread','Munchie Crunchie Cookie','Munchie Crunchie Rocky Road','Nutella Brownie','Orange & Pistachio Drizzle Traycake','Peanut Butter Choc Chunk Cookie','Rose Congou Leaf Tea','School Sprinkle Cake','Snickers Crispie','Sparkling Afternoon Tea for Two - Voucher','Sussex Apple Cake','Sussex Craggy Rooster','Sussex Cream Tea for One','Sussex Cream Tea for Two','Sussex Cream Tea For Two - Voucher','Traditional Afternoon Tea For Two - Voucher','Triple Chocolate Cookie','Vegan Apple, Sultana & Cinnamon Traycake','Victoria Sponge Cake','White Chocolate Millionaire Shortbread' ]; $('#our-products .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'products', limit: 20, source: substringMatcher(products) });