This is really tricky, because regular expressions are mostly about matching something that is there. With look-around trickery, you can do things like 'find A that is not preceded/followed by B', etc. But I think the most pragmatic solution for you wouldn't be that.

My proposal relies a little bit on your existing code not doing too crazy things, and you might have to fine-tune it, but I think it's a good shot, if you really want to use a RegEx-search for your problem.

So what I suggest would be to find all img tags, that can (but don't need to) have all valid attributes for an img-element. Whether that is an approach you can work with is for you to decide.

Proposal:

/&lt;img\s*((src|align|border|height|hspace|ismap|longdesc|usemap|vspace|width|class|dir|lang|style|title|id)="[^"]"\s*)*\s*\/?&gt;/


The current limitations are:


It expects your attribute values to be delimited by double quotes,
It doesn't take into account possible inline on*Event attributes,
It doesn't find img elements with 'illegal' attributes.


Using regular expressions to find img tags without an alt attribute
How do you score the Mayor in an unfinished city at the end of a game of Carcassonne?
