开发者

RegEx: matching a <tag> with random order attributes

开发者 https://www.devze.com 2023-03-12 17:49 出处:网络
I\'m trying to write a regular expression to match an <a> tag, containing an id and href in an unknown order.

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

精彩评论

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

关注公众号