方法一:
function fIsUrL(sUrl) { var sRegex = '^((https|http|ftp|rtsp|mms)?://)' + '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //ftp的user@ + '(([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 + '|' // 允許IP和DOMAIN(域名) + '([0-9a-z_!~*\'()-]+.)*' // 域名- www. + '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' // 二級域名 + '[a-z]{2,6})' // first level domain- .com or .museum + '(:[0-9]{1,4})?' // 埠- :80 + '((/?)|' // a slash isn't required if there is no file name + '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$'; var re = new RegExp(sRegex); //re.test() if (re.test(sUrl)) { return true; } return false; }
方法二:
function fCheckUrl(str) { var oRegUrl = new RegExp(); //aa.bb.com oRegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); if (!oRegUrl.test(str)) { return false; } return true; }
出處:http://www.jb51.net/article/31550.htm