Regex to match IMG tag that not contains src

Many times we get blank images that not comes with urls. If you are facing such problem and you want to remove the IMG html tag without src attribute or empty source tag, then you came to the right place. In this tutorial, we’ll cover all the conditions and problems.

You can also modify our regex to match other conditions like class tag or width tag.


Condition 1:

If your IMG tag not contains src attribute, then use following regex to match that tag.

<img(?![^>]\bsrc)[^>]?>

Check on Regex101

In the above regex, it will only select IMG tag that not contains src, but if the tag has empty src value it will not select that html tag.

Explanation of Regex

<img matches the characters <img literally (case sensitive)

Negative Lookahead (?![^>]*\bsrc)Assert that the Regex below does not match


Condition 2:

If the IMG tag has empty src attribute like this , <img src=””> then use the following regex to match that one.

<img((?:(?!<\/p>).)*?)src=""(.*?)>

Check on Regex101


You can modify the above regex codes to match img or other html tags that not contains a specific attribute. We can’t explain each and every conditions here, but we gave you the basic idea to match img without certain attributes.

Hope you found this article helpful. If you have any trouble about this problem ask us in the comment section, we’ll try to fix as soon as possible. To ask other regex, XPath, html, CSS, js questions , ask here.

Leave a Comment