JSFiddle - React, Tailwind, and code Playground
HTML
<div id='result'></div>
JavaScript
var re = new Regex("/<body.*?>(.*?)<\\/body.*?>/","ig");
var str = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body lang='en' xmlns='http://www.w3.org/1999/xhtml'>heello</body><body lang='de' xmlns='http://www.w3.org/1999/xhtml'>heello</body></html>";
var str2 = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body lang='en' xmlns='http://www.w3.org/1999/xhtml'>heello</body></html>";
function match1(regex, string) {
var matches1 = new Array();
var matches2 = [10];
var found;
while (found = regex.exec(string)) {
matches1.push(found[0]);
if (found.length>0)
{
matches2.push(found[1]);
}
regex.lastIndex = found.index + 1;
}
return [matches1,matches2];
};
function parseCode(c) {
for (var counter = 0; counter < c.length; counter++) {
result.append((counter + 1) + '.' + ' ' + c[counter]);
result.append('<br/>');
}
result.append('----');
result.append('<br/>');
};
var res1 = match1(re,str);
parseCode(res1);
var res2 = match1(re,str2);
parseCode(res2);