Skip to content Skip to sidebar Skip to footer

Regex Match Fails On String With Double Quotes

I have a string that dynamically comes from another document as follows; ' Preview &l

Solution 1:

You can try something like this:

/<body>(.*?)(?=<\/body>)/

This will start matching from <body> till the character followed by </body>.

Also since you are receiving HTMLString, you will not have multiple bodys and hence using match[0]

var s = '<!DOCTYPE html><html dir="ltr"><head><title>Preview</title></head><body><p>test</p><p><img alt="" height="299" src="http://172.0.0.1/Administration/YDImages/cap.JPG" width="696"></p></body></html>';

var regex = /<body>(.*?)(?=<\/body>)/;

var match = s.match(regex)
console.log(match)
var html = match[0].replace("<body>", "")

document.querySelector('.content').innerHTML = html
img{
  border: 1px solid gray;
}
<div class="content"></div>

Post a Comment for "Regex Match Fails On String With Double Quotes"