<body >
<a href="member.php?mod=logging&action=login&logsubmit=yes">
<img src="http://www.chinaunix.net/images_new/login.gif"></img></a>
&nbsp;&nbsp;<a href="http://sso.chinaunix.net/ForgotPassword?return_url=http://bbs.chinaunix.net/home.php?mod=spacecp">忘记密码</a>
&nbsp;&nbsp;<a href="member.php?mod=register.php">免费注册</a>
</td>
<td align="right">
<a href="http://bbs.chinaunix.net/listthread.php">查看新帖</a> |
<a href="http://www.chinaunix.net/jh/"><font color="red">论坛精华区</font></a>
</td>
</tr>
需要匹配以上内容中的
member.php?mod=logging&action=login&logsubmit=yes
member.php?mod=register.php
http://www.chinaunix.net/images_new/login.gif
http://sso.chinaunix.net/ForgotPassword?return_url=http://bbs.chinaunix.net/home.php?mod=spacecp
http://bbs.chinaunix.net/listthread.php
http://www.chinaunix.net/jh/这个正则怎么写啊

解决方案 »

  1.   

    完整的带http的url好办
      /http(s)?:\/\/[^\/]+(\/[^\s]*)?/不完整的url,是没有规律的,直接匹配无法做,
    建议将属性名字,例如href放一起考虑
    对于<a href="member.php?mod=logging&action=login&logsubmit=yes">
    可以写   /\bhref=['"]?[^'">\s]+['"]?/
      

  2.   

    <meta charset="utf-8"/>
    <textarea id="txt1" rows="15" cols="60">
      <body >
      <a href="member.php?mod=logging&action=login&logsubmit=yes">
      <img src="http://www.chinaunix.net/images_new/login.gif"></img></a>
      &nbsp;&nbsp;<a href="http://sso.chinaunix.net/ForgotPassword?return_url=http://bbs.chinaunix.net/home.php?mod=spacecp">忘记密码</a>
      &nbsp;&nbsp;<a href="member.php?mod=register.php">免费注册</a>
      </td>
      <td align="right">
      <a href="http://bbs.chinaunix.net/listthread.php">查看新帖</a> |
      <a href="http://www.chinaunix.net/jh/"><font color="red">论坛精华区</font></a>
      </td>
      </tr>
    </textarea><script>
    var txt = document.getElementById('txt1').value;
    var reg = /\s+(?:href|src)\s*=\s*"(.*?)"/gi;
    var m, a = [];while( m = reg.exec(txt) ){
      if(m[1]){ 
        a.push(m[1]) 
      }
    }alert(a.join('\n'));
    </script>