热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用正则表达式查找没有alt属性的img标记。-Usingregularexpressionstofindimgtagswithoutanaltattribute

Iamgoingthroughalargewebsite(1600+pages)tomakeitpassPriority1W3CWAI.Asaresult,th

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多页),使它通过优先级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在一个办公室。编辑>找到对话可以使用正则表达式。

6 个解决方案

#1


0  

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:

建议:

//

The current limitations are:

当前的局限性是:

  1. It expects your attribute values to be delimited by double quotes,
  2. 它期望您的属性值被双引号分隔,
  3. It doesn't take into account possible inline on*Event attributes,
  4. 它没有考虑到*事件属性的内联,
  5. It doesn't find img elements with 'illegal' attributes.
  6. 它没有发现含有“非法”属性的img元素。

#2


28  

Building on Mr.Black and Roberts126 answers:

基于布莱克先生和罗伯特126回答:

/(]*)(>)/

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标记的img标记或不后跟=""或="的alt标记(即无效的alt标记)。

Breaking it down:

分解:

(          : open capturing group
(['"])]*      : 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进行搜索和替换,那么您可以结合使用替换字符串:

$1 alt=""$3

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电子邮件等布局图片时非常有用。

#3


11  

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):

以下是我在自己的环境中尝试过的方法,使用大量的企业代码库,并取得了一些成功(没有发现假阳性,但肯定找到了有效的案例):

]*\balt=)[^>]*?>

What's going on in this search:

在这个搜索中发生了什么?

  1. find the opening of the tag
  2. 找到标签的开头
  3. look for the absence of zero or more characters that are not the closing bracket while also …
  4. 查找不属于结束括号的0或更多字符的缺失,同时……
  5. 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 …
  6. 检查是否缺少一个以“alt”开头的单词(“\b”是为了确保我们不会在类值之类的东西上获得一个中间单词的名称匹配),然后是“=”,然后……
  7. look for zero or more characters that are not the closing bracket
  8. 查找不是结束括号的零或多个字符
  9. find the closing bracket
  10. 找到个括号

So this will match:

这将匹配:


But it won't match either of these:

但这两者都不匹配:


I have a value.

#4


8  

This works in Eclipse:

这在Eclipse工作:

I'm updating for Section 508 too!

我也在更新第508节!

#5


6  

This worked for me.

这为我工作。

^

This matches any string beginning with that doesn't contain any number of characters before an alt attribute. It even works for src="" type of attributes.

它匹配任何以 ”类型的属性。

#6


0  

Simple and effective:

简单而有效:

This regex works for find tags missing the alt attribute.

这个regex用于查找标记,缺少alt属性。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • 小白的Python 学习笔记(八)推导式详解
    大家好,今天我总结一下Python的推导式,首先让我们来看定义推导式(comprehensions)是Python的一种独有特性,是可以从一个数据序列构建另一个新的数据序列的结构体 ... [详细]
  • TerraformVersionTerraformv0.10.6AffectedResource(s)Pleas ... [详细]
  • ThisisthecontinuedworkfromPR#2064takingincommentsfromhttps://do ... [详细]
  • 我是Python的新手,我正在用它进行一个难看的将表数据转换成db的小转换.该程序查看数据,在MySQL中创建一个表,然后将数据读入表中.在本节中,将检查标题行文本以对数据类型做出 ... [详细]
author-avatar
646579262
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有