开发者

RegEx: matching a <tag> with random order attributes

I'm trying to write a regular expression to match an <a> tag, containing an id and href in an unknown order.

The current regex I have right now is

/<[Aa]\s[^>]*(href="http:\/\/example.com"|id="badge")\s[^>]*(href="http:\/\/example.com"|id="badge")[^>]*>(.*)<\/a>/';

The code I'm matching on开发者_StackOverflow is

<a id="badge" target="_blank" href="http://example.com">
  <img src="">
</a>

Is there a more efficient way to match for the <a> tag?


The best way to match for the <a> tag would be to not use regex at all. You don't say which platform you are using, but I'll wager it has a decent HTML parser that you could leverage.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜