A few minor changes to the user-suggested code, less repetitive and cope when there are no opt parent groups:
$('select').select2({ matcher: function(term, text, opt){ var matcher = opt.parent('select').select2.defaults.matcher; return matcher(term, text) || (opt.parent('optgroup').length && matcher(term, opt.parent('optgroup').attr("label"))); } });
source share