How to show optgroup+label when selected

I want to show optgroup value with option value like this:

38

Previously I was using this JavaScript for showing selected label but unfortunately it’s not working on this plugin :
function format(item) {
opt = $(item.element);
sel = opt.text();
og = opt.closest(‘optgroup’).attr(‘label’);
return og+’ | '+item.text;
}
$(“select”).select2({
formatSelection: format,
escapeMarkup: function(m) { return m; }
});
$(".select2_sample3").select2({
tags: []
});

Please use Markdown to format your code block.