JSFiddle - React, Tailwind, and code Playground
JavaScript
var s = '<div><div><a href="link">Link</a><span> Span</span><li></li></div></div>';
var $s = $(s);
$s.find("*").not("a,img,br").each(function() {
$(this).replaceWith(this.innerHTML);
});
console.log('Wrong result: ' + $s.html());
var $s = $(s);
$s.find('*:not(br,a,img)').contents().unwrap();
console.log('Wrong result: ' + $s.html());
// Here is the correct pattern
var $s = $(s);
var $elements = $s.find("*").not("a,img,br");
for (var i = $elements.length - 1; i >= 0; i--) {
var e = $elements[i];
$(e).replaceWith(e.innerHTML);
}
console.log('Correct result: ' + $s.html());