Betamoo
Betamoo

Reputation: 15930

HTML Regex Composition

I am trying to capture img tag in HTML using Regex...

So these must be captured:

<img/>
< img id = "f" />

I have used:

"<\s*img(\s.*?)?/>"

But this goes wrong:

< img id = "/>" />

Any idea how to probably capture img tag??

Thanks

Upvotes: 0

Views: 183

Answers (3)

Shekhar
Shekhar

Reputation: 11788

You can use this regex

<\s*?img[\s\S]*?/>

Upvotes: 0

Core Xii
Core Xii

Reputation: 6441

"<\s*img\s(?:.+?\s*=\s*(\"|')?.*?\1\s*)?/>"

I think this should take the quotes into account. Didn't test it though.

Upvotes: 0

Femaref
Femaref

Reputation: 61467

On a serious note: Use an xml parser instead.

"<\simg\sid\s=\s\"(.*?)\"\s/>"

Also, you should look into using a regex testing suite like regex buddy.

This might be a good read as well: RegEx match open tags except XHTML self-contained tags

Upvotes: 2

Related Questions