Created
July 6, 2014 01:33
-
-
Save hehongwei44/ce075343fc539c1e4c75 to your computer and use it in GitHub Desktop.
判断输入的参数是否是个合格的URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @descrition:判断输入的参数是否是个合格的URL,由于url的灵活性和多样性,一下代码并不能测试所有的url都是合法的 | |
* @param:str->待判断的url参数 | |
* @return :true表示符合改正则。 | |
**/ | |
var isURL = function (str) { | |
var strRegex = "^((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 | |
+ "((/?)|" | |
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; | |
var re = new RegExp(strRegex); | |
return re.test(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
该正则无法通配所有的URL,请慎用