var console = {}; console.log = function(x) { print(x); } function doMatch(str1) { var regex1 = /<[^<>]*>/g; var array1; var nextPos = 0; var ret = []; while ((array1 = regex1.exec(str1)) !== null) { var matchStart = regex1.lastIndex - array1[0].length; if (nextPos < matchStart) { ret.push(str1.slice(nextPos, matchStart)); } ret.push(array1[0]); nextPos = regex1.lastIndex; } if (nextPos < str1.length - 1) { ret.push(str1.slice(nextPos)); } return ret; } console.log(doMatch('abc<def>hij</klm>nop<qrs>tuv</wxy>z')); console.log(doMatch('<def>hij</klm>nop<qrs></wxy>'));
Standard input is empty