var ems = (function() {
var re = /\<(\/?)em\>/g;
return function(str) {
var r = [], _, level = 0, last;
for (re.lastIndex = 0; _ = re.exec(str); )
if (!_[1]) {
if (!level++) last = re.lastIndex;
} else if (level && !--level)
r.push(str.substr(last, _.index - last));
if (level) r.push(str.substr(last));
return r;
};
})();
alert(ems('string <em>substring<em>substring</em>substring</em> xxx substring xxx <em>substring</em> xxx <em>substring</em>').join('\n'));