Html 正则表达式,其中img标记没有alt

Html 正则表达式,其中img标记没有alt,html,regex,image,alt,Html,Regex,Image,Alt,我正在尝试编写一个正则表达式来查找没有alt属性的img标记 这是正则表达式 <img\s+src.+^(?!alt).+$ 这是一些样品 <img src="smiley.gif" alt="Smiley face" height="42" width="42"> <img src="smiley2.gif" height="42" width="42"> <img src="smiley3.gif" alt="Smiley face Three" he

我正在尝试编写一个正则表达式来查找没有alt属性的img标记

这是正则表达式

<img\s+src.+^(?!alt).+$

这是一些样品

<img src="smiley.gif" alt="Smiley face" height="42" width="42">
<img src="smiley2.gif" height="42" width="42">
<img src="smiley3.gif" alt="Smiley face Three" height="42" width="42">

这里是指向regex101的链接

您可以使用以下表达式:

<img[^>]*alt=[^>]*>
]*alt=[^>]*>

如果在同一行中,您可以使用

<img(?!.*\s+alt\s*=).+$

您还没有指定使用哪种语言,但很可能您有一个可用的DOM解析器

例如,在JavaScript中,您可以执行以下操作:

var imgs_with_no_alt = document.querySelectorAll("img:not([alt])");
在PHP中,您需要以下内容:

$dom = new DOMDocument();
$dom->loadHTML("your HTML here");
$images = $dom->getElementsByTagName("img");
foreach($images as $image) {
    if( $image->getAttribute("alt")) continue;
    // do something to $image here, which doesn't have an [alt] attribute
}

尝试
]*?alt=)[^>]*>
会因为表达式失败而使您变得咸咸。请在
alt
之后添加
=
。e、 g.
]*alt=[^>]*>
]*\s+alt=[^>]*>
-使用正则表达式解析HTML不起作用!!!