Javascript正则表达式来验证URL [英] Javascript regular expression to validate URL
问题描述
我使用以下正则表达式验证网址。我也想验证 google.com ,但它会返回false。可以在RE下面更改 google.com 。
console.log(learnRegExp(的 'http://www.google-com.123')); // false
console.log(learnRegExp('https://www.google-com.com')); // true
console.log(learnRegExp('http://google-com.com')); // true
console.log(learnRegExp('http://google.com')); // true
console.log(learnRegExp('google.com')); // false
函数learnRegExp(){
return /^(ftp|https?):\/\//+(www\.??[a-z0-9 \-\] {3,} \ [AZ] {3} $ /测试(learnRegExp.arguments [0])。。。;
console.log('http://www.google-com。 123.com',validateUrl('http://www.google-com.123.com')); // true console.log('http://www.google-com.123',validateUrl('http://www.google-com.123')); // false console.log('https://www.google-com.com',validateUrl('https://www.google-com.com')); // true console.log('http://google-com.com',validateUrl('http://google-com.com')); // true console.log('http://google.com',validateUrl('http://google.com')); // true console.log('google.com',validateUrl('google.com')); //falseconsole.log('http://www.gfh。',validateUrl('http://www.gfh。')); //falseconsole.log('http://www.gfh.c',validateUrl('http://www.gfh.c')); //falseconsole.log('http://www.gfh:800000',validateUrl('http://www.gfh:800000')); //falseconsole.log('www.google.com',validateUrl('www.google.com')); //falseconsole.log('http://google',validateUrl('http:// google')); //falseconsole.log('//cdnblabla.cloudfront.net/css/app.css',validateUrl('// cdnblabla.cloudfront.net/css/app.css')); // truefunction validateUrl(value){return / ^(?:(?:( ?: https?| ftp):)?\ / \ /)(?: \S +(?:: \S *) ?@):|((?: 169\.254 | 192\\?(((?:10 127)(?: \.\d {1,3}){3}?!)! \\.168)(?: \.\d {1,3}){2})(?172\(?: 1 [6-9] |。2\d | 3 [0-1 ])(?: \.\d {1,3}){2})(?:?[1-9] \d | 1\d\d | 2 [01] \d | 22 [0-3])(?: \(?: 1 \d {1,2} |。Λ2 [0-4] \d | 25 [0-5])){2}(? :\(?:?[1-9] \d | 1\d\d | 2 [0-4] \d | 25 [0-4])。?)|(:( ?: [az\\\¡-\\\0-9] - *)* [az\\\¡-\\\0-9] +)(?: \(:?[az\\\¡-\\\0-9 ??] - *)* [az\\\¡-\\\0-9] +)*(?: \(:[az\\\¡-\\\] {2,})))(:: ?\d {2,5-})(:[?/#] \S *)$ / i.test(值);}
?
$ b
匹配 snippetdata-lang =jsdata-hide =falsedata-console =truedata-babel =false>
[// www.go ogle.com,//cdnblabla.cloudfront.net/css/app.css,http://✪df.ws/123,http:// userid:password@example.com:8080, http:// userid:password@example.com:8080 /,http://userid@example.com,http://userid@example.com/,http:// userid @ example .com:8080,http://userid@example.com:8080 /,http:// userid:password@example.com,http:// userid:password@example.com/, http://142.42.1.1/,http://142.42.1.1:8080/,http://➡.ws/䨹,http://⌘.ws,http:/ /⌘.ws/,http://foo.com/blah_(wikipedia)#cite-1,http://foo.com/blah_(wikipedia)_blah#cite-1,http:/ /foo.com/unicode_(✪)_in_parens,http://foo.com/(something)?after=parens,http://☺.damowmow.com/,http:// code。 google.com/events/#&product=browser,http://j.mp,ftp://foo.bar/baz,http://foo.bar/?q=Test% 20URL-encoded%20stuff,http://مثال.إختبار,http://例子。测试] .map(function(url){console.log(url,validateUrl(url));});函数validateUrl(value){return / ^(?:(?:( ?: https?| ftp):)?\ / \\ /)(?: \S +(:: \S *?)@)(:????!((?:10 | 127)(?: \.\d {1,3}) {3})((?: 169\.254?|!192\.168)(?: \.\d {1,3}){2})(?! 172\(? :1 [6-9] | 2\d | 3 [0-1])(?: \.\d {1,3}){2})(?:[1-9] \d ?| 1\d\d | 2 [01] \d | 22 [0-3])(?: \(?: 1 \d {1,2} |。?2 [0-4 ] \d | 25 [0-5])){2}(?: \(:?[1-9] \d | 1\d\d | 2 [0-4] \\ \\d | 25 [0-4]))|(:(:[az\\\¡-\\\0-9] - *)* [az\\\¡-\\\0-9] +)(???? :\(:[az\\\¡-\\\0-9] - *?)* [az\\\¡-\\\0-9] +)*(?: \(:。?[az\\ ?\¡-\\\] {2,})))(:: \d {2,5-})(:????[/#] \S *)$ / i.test(值) ;}
p> 所有这些来自 gist ,我希望这可以满足您的所有需求。 I am validating URL with following regular expression. I want to validate google.com also but it returns false. What can be changed in R.E below to validate google.com.
This validate the URL in general
Should Match
Should Fail
How it works All this comes from this gist, i hope that this fill all your needs 这篇关于Javascript正则表达式来验证URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
[http://,http://。, http:// ..,http://../,http://?,http:// ??,http:// ?? / ,http://#,http:// ##,http:// ## /,http://foo.bar?q=Spaces应该被编码,// // a,/// a,///,http:/// a,foo.com,rdar:// 1234,h:// test, http:// shouldfail.com,:// should fail,http://foo.bar/foo(bar)baz quux,ftps://foo.bar/,http:/ /-error-.invalid/,http:// - abco,http://ab-.co,http://0.0.0.0,http://10.1.1.0, http://10.1.1.255,http://224.1.1.1,http://1.1.1.1.1,http://123.123.123,http://3628126748, http://www.foo.bar/,http://www.foo.bar./,http://www.foo.bar./,http://10.1。 (函数url){console.log(url,validateUrl(url));}); function validateUrl(value){return / ^(?:(? :( ?: HTTPS | FTP):) \ / \ /)(?: \S +(:: \S *)@)(:???????!((?:10 | 127 )(?: \.\d {1,3}){3})((?: 169\.254 |?!192\.168)(?: \.\d {1, 3}){2})(?172\(?: 1 [6-9] |。2\d | 3 [0-1])(?: \.\d {1,3} ){2})(?:?。[1-9] \d | 1\d\d | 2 [01] \d | 22 [0-3])(?: \(?: 1 \d {1,2} | 2 [ 0-4] \d | 25 [0-5])){2}(?: \(:?[1-9] \d | 1\d\d | 2 [O- 4] \d | 25 [0-4]))|(?:?(:[az\\\¡-\\\0-9] - *)* [az\\\¡-\\\0-9] + )(?:\(:?[az\\\¡-\\\0-9] - *)。* [az\\\¡-\\\0-9] +)*(?: \(?: 〔az\\\¡-\\\] {2,})))(:: \d {2,5-})(:?[?/#] \S *)$ / i.test? (value);}
//协议标识符
(?:(?:( ?: https https?| ftp): )?//)
// user:pass authentication
(?:\\S +(?:: \\S *)?@)?
(?:
// IP地址排除
//私人&本地网络
(?!(?:10 | 127)(?: \\\) \\\\\\\\\\\\\\\\\\\\\\') \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\ 3(0-1))(?: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'网络0.0.0.0
//排除保留空间> = 224.0.0.0
//排除网络和广播地址
//(每个类的第一个和最后一个IP地址)
(?:[1-9] \\d?| 1\\\\\\\\\\\\\\\\\\\\\\\\ | | 2 2 2 2 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $?(?:\\。(?: 1?\\d {1,2} | 2 [0-4] \\d | 25 [0-5])){2}
(?:\\。(?:[1-9] \\d?| 1\\\\\\\\\\\\\\'| [0-4] \\\ \\ d | 25 [0-4]))
|
//主机名称
(?:(?:[az \\\\¡-\\ uffff0-9] * *)* [az \\\\¡-\\ffff0-9] +)
//域名
(?:\ \。(?:[az \\\\¡-\\\\\\\\\\\\\\\\\\')*
//?TLD标识符
(?:\\。(?:[az\\\\¡-\\ffff] {2,})))
//端口号
(?:: \\d {2,5})?
//资源路径
(?:[/?#] \\S *)?
console.log(learnRegExp('http://www.google-com.123')); // false
console.log(learnRegExp('https://www.google-com.com')); // true
console.log(learnRegExp('http://google-com.com')); // true
console.log(learnRegExp('http://google.com')); //true
console.log(learnRegExp('google.com')); //false
function learnRegExp(){
return /^(ftp|https?):\/\/+(www\.)?[a-z0-9\-\.]{3,}\.[a-z]{3}$/.test(learnRegExp.arguments[0]);
}
console.log('http://www.google-com.123.com', validateUrl('http://www.google-com.123.com')); // true
console.log('http://www.google-com.123', validateUrl('http://www.google-com.123')); // false
console.log('https://www.google-com.com', validateUrl('https://www.google-com.com')); // true
console.log('http://google-com.com', validateUrl('http://google-com.com')); // true
console.log('http://google.com', validateUrl('http://google.com')); //true
console.log('google.com', validateUrl('google.com')); //false
console.log('http://www.gfh.', validateUrl('http://www.gfh.')); //false
console.log('http://www.gfh.c', validateUrl('http://www.gfh.c')); //false
console.log('http://www.gfh:800000', validateUrl('http://www.gfh:800000')); //false
console.log('www.google.com ', validateUrl('www.google.com ')); //false
console.log('http://google', validateUrl('http://google')); //false
console.log('//cdnblabla.cloudfront.net/css/app.css', validateUrl('//cdnblabla.cloudfront.net/css/app.css')); //true
function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
}
["//www.google.com", "//cdnblabla.cloudfront.net/css/app.css", "http://✪df.ws/123", "http://userid:password@example.com:8080", "http://userid:password@example.com:8080/", "http://userid@example.com", "http://userid@example.com/", "http://userid@example.com:8080", "http://userid@example.com:8080/", "http://userid:password@example.com", "http://userid:password@example.com/", "http://142.42.1.1/", "http://142.42.1.1:8080/", "http://➡.ws/䨹", "http://⌘.ws", "http://⌘.ws/", "http://foo.com/blah_(wikipedia)#cite-1", "http://foo.com/blah_(wikipedia)_blah#cite-1", "http://foo.com/unicode_(✪)_in_parens", "http://foo.com/(something)?after=parens", "http://☺.damowmow.com/", "http://code.google.com/events/#&product=browser", "http://j.mp", "ftp://foo.bar/baz", "http://foo.bar/?q=Test%20URL-encoded%20stuff", "http://مثال.إختبار", "http://例子.测试"].map(function(url) {
console.log(url, validateUrl(url));
});
function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
}
["http://", "http://.", "http://..", "http://../", "http://?", "http://??", "http://??/", "http://#", "http://##", "http://##/", "http://foo.bar?q=Spaces should be encoded", "//", "//a", "///a", "///", "http:///a", "foo.com", "rdar://1234", "h://test", "http:// shouldfail.com", ":// should fail", "http://foo.bar/foo(bar)baz quux", "ftps://foo.bar/", "http://-error-.invalid/", "http://-a.b.co", "http://a.b-.co", "http://0.0.0.0", "http://10.1.1.0", "http://10.1.1.255", "http://224.1.1.1", "http://1.1.1.1.1", "http://123.123.123", "http://3628126748", "http://.www.foo.bar/", "http://www.foo.bar./", "http://.www.foo.bar./", "http://10.1.1.1", "http://10.1.1.254"].map(function(url) {
console.log(url, validateUrl(url));
});
function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
}
// protocol identifier
"(?:(?:(?:https?|ftp):)?//)"
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?"
"(?:"
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})"
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})"
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})"
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
"|"
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)"
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))"
// port number
"(?::\\d{2,5})?"
// resource path
"(?:[/?#]\\S*)?"