当前位置: 编程技术>其它
php匹配网址的正则 几乎可以匹配任何网址
来源: 互联网 发布时间:2014-10-17
本文导语: 几乎可以匹配任何网址: 代码如下: ^((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]))...
几乎可以匹配任何网址:
^((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+_-.%=&]*)?)?(#[a-z][a-z0-9_]*)?$
实例:
function IsURL(/tech-program-other/urlString/index.html)
{
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+_-.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/
if (urlString.match(regExp))
return "is a url";
else
return "not a url";
}
url = "";
alert(IsURL(/tech-program-other/url/index.html));
代码如下:
^((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+_-.%=&]*)?)?(#[a-z][a-z0-9_]*)?$
实例:
代码如下:
function IsURL(/tech-program-other/urlString/index.html)
{
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+_-.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/
if (urlString.match(regExp))
return "is a url";
else
return "not a url";
}
url = "";
alert(IsURL(/tech-program-other/url/index.html));