关闭所有 HTML 未关闭的 IMG 标签 [英] Close all HTML unclosed IMG tags

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

问题描述

是否可以对所有未关闭的 IMG 标签进行正则表达式替换?如果是这样,我将如何识别:

Is it possible to do a regex replace on all IMG tags that are unclosed? If so, how would I identify:

  <img src="..." alt="...">

...作为可能被替换的候选者?

...as a potential canidate to be replaced?

   = <img src="..." alt="..."/>

更新:我们有数百个页面和数千个图像标签,所有这些标签都必须关闭.我并没有坚持使用 RegEx——除了手动更新所有 IMG 标签之外,任何其他方法都足够了.

Update: We have hundreds of pages, and thousands of image tags, all must of which must be closed. I'm not stuck on RegEx -- any other method, aside from manually updating all IMG tags, would suffice.

推荐答案

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

将匹配未正确关闭的 img 标签.它要求您使用的正则表达式风格支持后视(Ruby 和 JavaScript 不支持,但其他大多数支持).反向引用编号1 将包含匹配项,因此如果您搜索此正则表达式并替换为 \1/>,您应该很高兴.

will match an img tag that is not properly closed. It requires that the regex flavor you're using supports lookbehind (which Ruby and JavaScript don't but most others do). Backreference no. 1 will contain the match, so if you search for this regex and replace by \1/> you should be good to go.

如果您需要考虑> 内部属性的可能性,您可以使用

If you need to account for the possibility of > inside attributes, you could use

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

这将匹配,例如,

<img src="image.gif" alt="hey, look--->">
<img src="image/image.gif">

离开

<img src="image/image.gif" />

一个人.

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

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