Haven't checked whether there is a more optimal way to do it, but there you go
var code =
'Lorem Ipsum<p>Foo Bar</p><div class="myclass"></div><p>Foo Bar</p><div class="myclass"></div>'
var $object = $('<div/>').html(code);
var $obj = $();
$object.contents().each(function () {
if ($(this).hasClass('myclass')) {
$obj.wrapAll('<div class="wrap"/>');
$obj = $();
} else {
$obj = $obj.add(this)
}
})
$obj.wrapAll('<div class="wrap"/>');
Demo: Fiddle