开发者

HTML Regex Composition

开发者 https://www.devze.com 2023-01-18 18:46 出处:网络
I am trying to capture开发者_JAVA百科 img tag in HTML using Regex... So these must be captured: <img/>

I am trying to capture开发者_JAVA百科 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


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


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

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


You can use this regex

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号