2

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.

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

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

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

1 Answers1

0

If jQuery is an option:

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.