0

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

Betamoo
  • 14,964
  • 25
  • 75
  • 109
  • 1
    Which flavor of regex are you using, Perl, JavaScript, .NET...? And do you really have tags with whitespace between opening angle bracket and the tag name? I don't think that's legal in HTML. – Alan Moore Oct 06 '10 at 11:48

3 Answers3

2

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

Community
  • 1
  • 1
Femaref
  • 60,705
  • 7
  • 138
  • 176
0
"<\s*img\s(?:.+?\s*=\s*(\"|')?.*?\1\s*)?/>"

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

Core Xii
  • 6,270
  • 4
  • 31
  • 42
0

You can use this regex

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

Shekhar
  • 11,438
  • 36
  • 130
  • 186