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','Chai Tea Loaf','Chocolate Brownie (pack of four)','Chocolate Chunk Cookies (box of 4)','Chocolate Orange Cookies (box of four)','Cookies & Cream Rocky Road','Earl Grey Leaf Tea','Empire Biscuits','English Breakfast Leaf Tea','Kinder Chocolate Cookies (box of 4)','Malt Crunch Cookies (box of four)','Mars Bar Cookie (box of 4)','Masala Chai Leaf Tea','Millionaire Shortbread (box of 4)','Mint Chip Cookies (box of four)','Munchie Crunchie Cookie','Peanut Butter Cookies (box of four)','Rose Congou Leaf Tea','Shortbread Biscuit (box of six)','Smartie Cookies (box of four)','Sparkling Afternoon Tea for Two - Voucher','Sussex Apple Cake','Sussex Craggy Roosters (box of four)','Sussex Cream Tea for Two','Sussex Cream Tea For Two - Voucher','Traditional Afternoon Tea For Two - Voucher','Triple Chocolate Cookies (box of 4)','Victoria Sponge','White Chocolate Millionaire Shortbread (box of 4)' ]; $('#our-products .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'products', limit: 20, source: substringMatcher(products) });