如何使用JS从HTML字符串中删除结束标记 [英] How to remove the end tag from a HTML string using JS
本文介绍了如何使用JS从HTML字符串中删除结束标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除给定HTML字符串的结束标记.我从文档中获取HTML字符串,如下所示:
I want to remove the end tag of a given HTML string. I get the HTML string from the document like so:
var front = $('<div>').append($(clean_copy).contents().remove().end()).remove().html();
我现在要从下面删除</div>
:
<div id="div1" style="font-size: 20px"></div>
我对regex真的很生疏,但我想尝试这样的事情:
I am really rusty with regex but I want to try something like this:
console.log("Remove End: " + front.replace('/<(/)?div([^>]*)>/g', ''));
以上操作无效,如何删除任何标签?另外,某些字符串可能没有结束标签.
The above doesn't work, how can I remove any tag? Also some strings may not have an end tag.
推荐答案
尝试front.replace(/<\/div>$/)
它取代了您的div.
如果您想替换任何最后一个结束标签,请使用此front.replace(/<\/\S+>$/)
It replaces your div.
If you like to replace any last end tag use this front.replace(/<\/\S+>$/)
这篇关于如何使用JS从HTML字符串中删除结束标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文