使用正则表达式查找没有alt属性的img标签 [英] Using regular expressions to find img tags without an alt attribute

查看:355
本文介绍了使用正则表达式查找没有alt属性的img标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览一个大型网站(1600多个页面),以使其通过Priority 1 W3C WAI。结果,像图像标签之类的东西需要具有alt属性。

I am going through a large website (1600+ pages) to make it pass Priority 1 W3C WAI. As a result, things like image tags need to have alt attributes.

查找没有alt属性的img标签的正则表达式是什么?如果可能的话,请带些微的解释,以便我可以使用它来查找其他问题。

What would be the regular expression for finding img tags without alt attributes? If possible, with a wee explanation so I can use to find other issues.

我现在在使用Visual Web Developer 2008的办公室中。编辑>>查找对话框可以使用

I am in an office with Visual Web Developer 2008. The Edit >> Find dialogue can use regular expressions.

推荐答案

这确实很棘手,因为正则表达式主要是匹配存在的内容。借助环顾四周的技巧,您可以执行类似的操作,例如找到A后面没有B的A等。但是,我认为最实用的解决方案不是。

This is really tricky, because regular expressions are mostly about matching something that is there. With look-around trickery, you can do things like 'find A that is not preceded/followed by B', etc. But I think the most pragmatic solution for you wouldn't be that.

我的建议稍微依赖于您现有的代码,而不必做过于疯狂的事情,您可能需要对其进行微调,但如果您真的想使用RegEx搜索,那么我认为这是一个不错的选择

My proposal relies a little bit on your existing code not doing too crazy things, and you might have to fine-tune it, but I think it's a good shot, if you really want to use a RegEx-search for your problem.

所以我建议是找到所有img标签,可以(但不需要)包含所有img元素的有效属性。

So what I suggest would be to find all img tags, that can (but don't need to) have all valid attributes for an img-element. Whether that is an approach you can work with is for you to decide.

建议:

/<img\s*((src|align|border|height|hspace|ismap|longdesc|usemap|vspace|width|class|dir|lang|style|title|id)="[^"]"\s*)*\s*\/?>/

当前限制为:


  1. 它期望您的属性值由双引号分隔,

  2. 它未考虑可能的内联on * Event属性,

  3. 它未找到具有非法属性的img元素。

这篇关于使用正则表达式查找没有alt属性的img标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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