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 = ['[City & Guilds Confined space Low-Risk]','1 Day Refresher Site Supervision Safety Training Scheme (SSSTS)','City & Guilds Confined Space High-Risk','City & Guilds Confined Space Medium Risk','CPCS A09 Forward Tipping Dumper (A: Wheeled) - Experienced: 2 Days','CPCS A09 Forward Tipping Dumper/Ride-on-Roller - Experienced: 2 Days','CPCS A09 Forward Tipping Dumper/Ride-on-Roller - Novice: 4 Days','CPCS A17 Telescopic Handler (C: All Sizes exc. Slew) - Experienced: 2 Days','CPCS A17 Telescopic Handler (C: All Sizes exc. Slew) - Novice: 5 Days','CPCS A31 Ride on Roller','CPCS A40 Slinger/Signaller (A: All Types & Duties) - Experienced: 2 Days','CPCS A40 Slinger/Signaller (A: All Types & Duties) - Experienced: 3 Days','CPCS A40 Slinger/Signaller (A: All Types & Duties) - Foundation: 4 Days','CPCS A40 Slinger/Signaller (A: All Types & Duties) - Novice: 3 Days','CPCS A58 Excavator (360 Tracked, Below 10T) - Novice: 5 Days','CPCS A59 Excavator (360 - Tracked, Above & Below 10T) - Experienced: 2 Days','CPCS A59 Excavator (360 - Tracked, Above & Below 10T) - Novice: 5 Days','CPCS Lorry Loader A36','EXCAVATOR (360 TRACKED, BELOW 10T) - EXPERIENCED: 2 DAYS -','NRSW Operative & Supivisor','Qualsafe - 1 Day Emergency First Aid at work','Qualsafe 3 Day First Aid at Work','Refresher 1 Day course Site Manager Safety Training Scheme (SMSTS)','Site Manager Safety Training Scheme SMSTS','Telescopic Handler (C: All Sizes exc. Slew) - Foundation: 5 Days','Telescopic Handler (C: All Sizes exc. Slew) -Semi-Experienced: 2 Days','Telescopic Handler (C: All Sizes exc. Slew) -Semi-Experienced: 3 Days' ]; $('#our-products .typeahead').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'products', limit: 20, source: substringMatcher(products) });