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

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

问题描述

我正在浏览一个大型网站(1600 多页)以使其通过优先级 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.

推荐答案

这真的很棘手,因为正则表达式主要用于匹配存在的东西.使用环视技巧,您可以执行诸如查找 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.

建议:

/<imgs*((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天全站免登陆