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.
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.
I am in an office with Visual Web Developer 2008. The Edit >> Find dialogue can use regular expressions.
The required alt attribute specifies an alternate text for an image, if the image cannot be displayed. The alt attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).
The src attribute is required, and contains the path to the image you want to embed. The alt attribute holds a text description of the image, which isn't mandatory but is incredibly useful for accessibility — screen readers read this description out to their users so they know what the image means.
Alt text uses: Visually impaired users using screen readers will be read an alt attribute to better understand an on-page image. 2. Alt text will be displayed in place of an image if an image file cannot be loaded.
Well, the alt tag is really a misnomer and doesn't exist at all. Alt text, or alternative text, is the alternate text attribute of the image tag.
Building on Mr.Black and Roberts126 answers:
/(<img(?!.*?alt=(['"]).*?\2)[^>]*)(>)/
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).
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 \2) : 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:
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With