正则表达式删除空白的html标签,它只包含空的子标签 [英] Regex to remove empty html tags, that contains only empty children

查看:99
本文介绍了正则表达式删除空白的html标签,它只包含空的子标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

示例:

 < p ALIGN = 左 >< FONT FACE = Arial字体 SIZE = 12 COLOR = #000000 的字母间距= 0 字距=1 >< B个;< / B>< / FONT>< / P> 

不包含任何信息,必须替换为< / br>

我写了一个这样的正则表达式: >< \w + \b [^>] *>(小于\w + \b [^>] *> \s * LT; / * \w * \s> ;)* LT; / * \w * \s>

但问题在于它只捕捉三个层次中的两个层次。在abobe示例中,未选中< p> 元素(最外面的元素)。 ?你能帮我解决这个表达式


解决方案

这正则表达式似乎工作:



'pre> /(≤(?!\ /)[^>] +>)+(小于\ / [^>] +>)+ /

查看现场演示与您的示例。


I need to parse an HTML string and remove all the elements which contain only empty children.

Example:

<P ALIGN="left"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="1"><B></B></FONT></P>

contains no information and must be replaced with </br>

I wrote a regex like this:

<\w+\b[^>]*>(<\w+\b[^>]*>\s*</\w*\s*>)*</\w*\s*>

but the problem is that it's catching only 2 levels of the three. In the abobe example, the <p> element (the outer-most one) is not selected.

Can you help me fix this regex?

解决方案

This regex seems to work:

/(<(?!\/)[^>]+>)+(<\/[^>]+>)+/

See a live demo with your example.

这篇关于正则表达式删除空白的html标签,它只包含空的子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆