php匹配网址的正则 几乎可以匹配任何网址

<p>比较简单的</p><pre class="brush:php;toolbar:false">$str=&#39;&#39;; $isMatched=preg_match(&#39;/^^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+$/&#39;,$str,$matches); var_dump($isMatched,$matches);</pre><p>几乎可以匹配任何网址:</p><pre class="brush:php;toolbar:false">^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$</pre><p>实例:</p><pre class="brush:js;toolbar:false">&lt;scripttype=&quot;text/javascript&quot;&gt; functionIsURL(urlString) { regExp=/^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$/ if(urlString.match(regExp)) return&quot;isaurl&quot;; else return&quot;notaurl&quot;; } url=&quot;&quot;; alert(IsURL(url)); &lt;/script&gt;</pre>
RangeTime:0.006318s
RangeMem:204.81 KB
返回顶部 留言