Search code examples
htmlregeximagealt

REGEX where img tag does not have alt


I am trying to write a regex that looks for an img tag where there is no alt attribute.

Here is the regex.

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

Here are some samples

<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">

Here is a link to regex101 https://regex101.com/r/Z5vkQb/3/


Solution

  • If within the same line, you can use this,

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

    Demo