【发布时间】:2021-09-08 16:04:40
【问题描述】:
我的字符串
xxx
<div>
test test
</div>
</div>
<h2> Details</h2>
xxx
我想把整个 HTML 字符串替换成这样
xxx
<div>
test test
</div>
<h2> Details</h2>
xxx
这意味着没有这个额外的“</div>”
我已经尝试过,但不起作用:
result = result.replace('</div>\n<h2> Details</h2>', '<h2> Details</h2>');
或使用正则表达式,但“测试测试”也被删除
const excessDiv = /<\/div>(?:.|\n|\r)+?<h2> Details<\/h2>/;
result = result.replace(excessDiv, '</div><h2> Details</h2>');
有人可以推荐我吗?
【问题讨论】:
-
希望这篇对你有帮助:stackoverflow.com/questions/15587408/…
-
谢谢,但我不能使用 jquery :(
标签: javascript html regex dom replace