关闭所有HTML标签(不仅是IMG) [英] Close all HTML tags (not only IMG)

查看:49
本文介绍了关闭所有HTML标签(不仅是IMG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了关于关闭img标签的问答.

I saw this question and answer regarding closing img tags.

但是,如果我也想关闭链接等其他标签怎么办?

However, what if I want to close also other tags like link?

我试图写

(<img|link[^>]+)(?<!/)>

但这是行不通的

怎么了?

示例:

<link href="myhref">
<img src="mysrc">

但不是

<link href="myhref"/>
<img src="mysrc"/>

推荐答案

您需要限制交替的范围.否则,< 仅在交替的左边匹配时才匹配,而 [^>] + 仅在右边匹配时才匹配.

You need to limit the scope of your alternation. Otherwise the < will only be matched if the left part of the alternation matches, and [^>]+ will only be matched if the right part does.

(<(?:img|link)[^>]+)(?<!/)>

应解决此问题.(?:...)是一个非捕获组,即.例如,仅用于分组,而不用于捕获.替换操作(使用 \ 1/> )保持不变.

should fix this problem. (?:...) is a non-capturing group, i. e., just used for grouping, not for capturing. The replace operation (with \1/>) remains the same.

这篇关于关闭所有HTML标签(不仅是IMG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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