How would I match images that is not nested inside an anchor tag using regular expression?
Here is what I want:
No match: <a href="index.html"><img src="images/default.jpg" /></a>
Match: <div><img src="images/default.jpg" /></div>
Match: <img src="images/default.jpg" />
I’m no good at regex but this is what I came up so far, which doesn’t work:
[^<a[^>]*>]<img.*?/>[^</a>]
I couldn’t use lookarounds since PHP wants it to be specific.
Much of the reason behind your difficulty is simply that HTML is not a regular language, see: Coding Horror: Parsing Html the Cthulhu Way
Consider using a query expression language powerful enough to process (X)HTML, or just using the DOM programmatically to fetch all image tags and then exclude those with
<a>ancestors.In PHP5, I believe you can use
DOMXPath, using that it becomes as simple as:This code would give the output: