Regex不是最好的选择。如果你处理的是HTML,那么最好使用JS的查询功能,下面是一个简单的例子,说明你可以做什么(为了解释的目的,过于简单化了):
var htmlString = '<p><div class="something"><span><div class="else">My precious text.</div></span></div></p>';
//Create a DOM element so you can query what you need and manipulate it
var newElement = document.createElement("p");
newElement.innerHTML = htmlString;
//Find what you need to remove
var toRemove = newElement.getElementsByClassName("something")[0];
//Grab what you need to keep
var toKeep = toRemove.firstChild;
//Remove the unwanted element
newElement.removeChild(toRemove);
//Append the old child
newElement.appendChild(toKeep);
//If you really want it back as a string
newElement.outerHTML;
注意:在段落元素中使用div是无效的,因此您将得到不可预测的结果。