How get <body> element from the html which one have as a string

And the must-have non-jQuery answer:

 var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];

This will return only whats inside the body tags.

UPDATE this accepts the attributes set on the body tag


Another way to do this, without jQuery:

function getStupidErrorMessage(str) {
  var bodyTags = str.match(/<\/*body[^>]*>/gim);
  // returns an array
  // bodyTags[0] is body open, bodyTags[1] is body close
  // unless someone output the markup backwards :)
  bodyContents = str.slice(bodyTags[0].length,-(bodyTags[1].length));
  return bodyContents; // use as innerHTML of <body> 
}

If you need the attributes of the BODY tag, parse those as well.