visual-studio 使用正则表达式查找没有 alt 属性的 img 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4031948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using regular expressions to find img tags without an alt attribute
提问by awrigley
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.
我正在浏览一个大型网站(1600 多页)以使其通过 Priority 1 W3C WAI。因此,像图像标签这样的东西需要有 alt 属性。
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.
查找没有 alt 属性的 img 标签的正则表达式是什么?如果可能,请进行简单的解释,以便我可以用来查找其他问题。
I am in an office with Visual Web Developer 2008. The Edit >> Find dialogue can use regular expressions.
我在 Visual Web Developer 2008 的办公室。编辑 >> 查找对话框可以使用正则表达式。
采纳答案by Thomas
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.
这真的很棘手,因为正则表达式主要是关于匹配存在的东西。通过环视技巧,您可以执行诸如“查找 B 之前/之后没有的 A”之类的操作。但我认为对您来说最实用的解决方案不是那样。
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.
我的建议有点依赖于您现有的代码,不要做太疯狂的事情,您可能需要对其进行微调,但我认为这是一个很好的尝试,如果您真的想使用 RegEx 搜索来解决您的问题。
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 标签,这些标签可以(但不需要)具有 img 元素的所有有效属性。这是否是您可以使用的方法由您决定。
Proposal:
提议:
/<img\s*((src|align|border|height|hspace|ismap|longdesc|usemap|vspace|width|class|dir|lang|style|title|id)="[^"]"\s*)*\s*\/?>/
The current limitations are:
目前的限制是:
- It expects your attribute values to be delimited by double quotes,
- It doesn't take into account possible inline on*Event attributes,
- It doesn't find img elements with 'illegal' attributes.
- 它期望您的属性值由双引号分隔,
- 它没有考虑可能的内联 on*Event 属性,
- 它找不到具有“非法”属性的 img 元素。
回答by Gruffy
Building on Mr.Black and Roberts126 answers:
以 Mr.Black 和 Roberts126 的回答为基础:
/(<img(?!.*?alt=(['"]).*?)[^>]*)(>)/
This will match an img tag anywhere in the code which either has no alt tag or an alt tag which is not followed by ="" or ='' (i.e. invalid alt tags).
这将匹配代码中任何没有 alt 标签或后面没有 ="" 或 ='' 的 alt 标签(即无效的 alt 标签)的 img 标签。
Breaking it down:
分解它:
( : open capturing group
<img : match the opening of an img tag
(?! : open negative look-ahead
.*? : lazy some or none to match any character
alt=(['"]) : match an 'alt' attribute followed by ' or " (and remember which for later)
.*? : lazy some or none to match the value of the 'alt' attribute
) : back-reference to the ' or " matched earlier
[^>]* : match anything following the alt tag up to the closing '>' of the img tag
) : close capturing group
(>) : match the closing '>' of the img tag
If your code editor allows search and replace by Regex you can use this in combination with the replace string:
如果您的代码编辑器允许通过 Regex 进行搜索和替换,您可以将其与替换字符串结合使用:
alt=""
To find any alt-less img tags and append them with an empty alt tag. This is useful when using spacers or other layout images for HTML emails and the like.
查找任何没有 alt 的 img 标签并在它们后面附加一个空的 alt 标签。这在为 HTML 电子邮件等使用分隔符或其他布局图像时非常有用。
回答by squareman
Here is what I just tried in my own environment with a massive enterprise code base with some good success (found no false positives but definitely found valid cases):
这是我刚刚在我自己的环境中尝试过的,拥有大量企业代码库并取得了一些成功(没有发现误报,但确实找到了有效案例):
<img(?![^>]*\balt=)[^>]*?>
What's going on in this search:
此搜索中发生了什么:
- find the opening of the tag
- look for the absence of zero or more characters that are not the closing bracket while also …
- Checking for the absence of of a word that begins with "alt" ("\b" is there for making sure we don't get a mid-word name match on something like a class value) and is followed by "=", then …
- look for zero or more characters that are not the closing bracket
- find the closing bracket
- 找到标签的开头
- 寻找不存在零个或多个不是右括号的字符,同时……
- 检查是否缺少以“alt”开头的单词(“\b”是为了确保我们不会在诸如类值之类的东西上获得中间词名称匹配)并且后跟“=”,然后 …
- 查找不是右括号的零个或多个字符
- 找到右括号
So this will match:
所以这将匹配:
<img src="foo.jpg" class="baltic" />
But it won't match either of these:
但它不会匹配以下任何一个:
<img src="foo.jpg" class="baltic" alt="" />
<img src="foo.jpg" alt="I have a value.">
回答by Mr.Black
This works in Eclipse:
这适用于 Eclipse:
<img(?!.*alt).*?>
<img(?!.*alt).*?>
I'm updating for Section 508 too!
我也在更新第 508 节!
回答by Roberts126
This worked for me.
这对我有用。
^<img(?!.*alt).*$
This matches any string beginning with <imgthat doesn't contain any number of characters before an alt attribute. It even works for src="<?php echo $imagename; ?>"type of attributes.
这匹配任何<img以 alt 属性前不包含任何数量字符的字符串开头。它甚至适用于src="<?php echo $imagename; ?>"属性类型。
回答by edercortes
Simple and effective:
简单有效:
<img((?!\salt=).)*?
<img((?!\salt=).)*?
This regex works for find <img>tags missing the altattribute.
此正则表达式适用于查找<img>缺少该alt属性的标签。
回答by Niels Vanhorenbeeck
This is perfectly possible with following regEx:
使用以下正则表达式完全有可能:
<img([^a]|a[^l]|al[^t]|alt[^=])*?/>
Looking for something that isn't there, is rather tricky, but we can trick them back, by looking for a group that doesn't start with 'a', or an 'a' that doesn't get followed by an 'l' and so on.
寻找不存在的东西是相当棘手的,但我们可以通过寻找不以 'a' 开头的组或后面没有 'l' 的 'a' 来欺骗他们' 等等。
回答by Nagibaba
I wrote a simple code for this without Regex
我为此编写了一个没有正则表达式的简单代码
let arr = []
$('img')
.filter(function() {
arr.push(this.alt)
})
document.write(arr.filter(a=>!a).length + ' img without alt tag')

