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

1
  • 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. Commented Oct 6, 2010 at 11:48

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

0

You can use this regex

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.