如何计算img alt标签没有值,没有alt标签img标签与正则表达式

时间:2022-11-27 18:18:38

getting compilation error,saying Only assignment, call, increment, decrement, and new object expressions can be used as a statement and ; Expected. how to count img tags having no alt attributes as well as img tags with an alt attribute containing an empty string.

得到编译错误,说只有赋值,调用,递增,递减和新对象表达式才能用作语句;预期。如何计算没有alt属性的img标签以及带有包含空字符串的alt属性的img标签。

Both img tags below should be counted, but only the second one is counted in my code:

下面的两个img标签都应该计算,但只有第二个标记在我的代码中计算:

<img src="http://google.com/images/38524_1105/16/geolocate.png" class="absmiddle" alt="" />

<img src="http://google.com/images/38524_1105/16/geolocate.png" class="absmiddle" />

Here's my code (my aspx.cs file):

这是我的代码(我的aspx.cs文件):

 MatchCollection ImgAltTag = Regex.Matches(strIn, "<img[^>]*alt=['"].+['"]", RegexOptions.IgnoreCase | RegexOptions.Multiline);

1 个解决方案

#1


0  

If jQuery is an option:

如果jQuery是一个选项:

var count = $('img').filter(function(){
  return !$(this).attr('alt'); // false when contains alt attribute
}).length;

document.write(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<img src="alt.png" class="absmiddle" alt="" />
<img src="alt.png" class="absmiddle" />


But, asking your question, here is your regex:

但是,问你的问题,这是你的正则表达式:

<img[^>]*alt=['"].+['"]

Regex live here.

正则表达式住在这里。


Hope one of them could help.

希望其中一人可以提供帮助。

#1


0  

If jQuery is an option:

如果jQuery是一个选项:

var count = $('img').filter(function(){
  return !$(this).attr('alt'); // false when contains alt attribute
}).length;

document.write(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<img src="alt.png" class="absmiddle" alt="" />
<img src="alt.png" class="absmiddle" />


But, asking your question, here is your regex:

但是,问你的问题,这是你的正则表达式:

<img[^>]*alt=['"].+['"]

Regex live here.

正则表达式住在这里。


Hope one of them could help.

希望其中一人可以提供帮助。