网络编程 
首页 > 网络编程 > 浏览文章

js通过正则匹配没有内容的空标签

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

js 如何正则匹配没有内容的空标签并移除掉"htmlcode">

html='<div id="fixedTools" class="hidden-xs hidden-sm">'+
'\n  <a id="backtop" class="hidden border-bottom" href="#" rel="external nofollow" ></a>'+
'\n'+
'\n  <div class="qrcodeWraper">'+
'\n    <a href="/app#qrcode" rel="external nofollow" ><span class="glyphicon glyphicon-qrcode"></span></a>'+
'\n    <img id="qrcode" class="border" alt="sf-wechat" src="/UploadFiles/2021-04-02/appQrcode.png">

如果考虑将没有style的span去掉

有span的就留下来

js通过正则匹配没有内容的空标签

因为默认编辑器中,span没有样式的没有必要

str=str.replace(/<span\s*"$1");

先看下面的位置

零宽断言
接下来的四个用于查找在某些内容(但并不包括这些内容)之前或之后的东西,也就是说它们像\b,^,$那样用于指定一个位置,这个位置应该满足一定的条件(即断言),因此它们也被称为零宽断言。最好还是拿例子来说明吧:

断言用来声明一个应该为真的事实。正则表达式中只有当断言为真时才会继续进行匹配。

("htmlcode">

//加强替换主要是考虑多个br的问题
function doRepAdvance(s){
var str=s.replace(/<p><br type="_moz">\s*"");
str=str.replace(/<p>\s*<br type="_moz">\s*<\/p>/ig, "");
str=str.replace(/<p>\s*"");
str=str.replace(/<p>(\s|\&nbsp\;| | |\xc2\xa0)*<\/p>/ig, "");
str=str.replace(/<p>\s*"");
str=str.replace(/<p>&nbsp;<\/p>/ig,"");
str=str.replace(/<br type="_moz">\n&nbsp;<\/p>/ig, "</p>");
str=str.replace(/<br type="_moz">\s*"</p>");
str=str.replace(/<br\s"</p>");
str=str.replace(/<br \/>\n&nbsp;<\/p>/ig, "</p>");
str=str.replace(/<br>\n&nbsp;<\/p>/ig, "</p>");
//多个br
str=str.replace(/(<br type="_moz">\s*)+<\/p>/ig, "</p>");
str=str.replace(/(<br\s"</p>");
//空标签
str=str.replace(/<p style=["'].[^<>]*["']>/ig, "<p>");
str=str.replace(/<span style="background-color: initial;">/ig, "<span>");
//没有style的span去掉
str=str.replace(/<span\s*"$1");
str=str.replace(/<([a-z]+"");
//str=str.replace(/<([a-z]+"");

return str;
}

上面都是一些好东西,具体的自己研究吧。

上一篇:超强变态的正则(\w)((?=\1\1\1)(\1))+讲解
下一篇:js处理网页编辑器转义、去除转义、去除HTML标签的正则
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网