示例:% w% V9 u5 r8 [8 B) Z4 G8 d' E
http://www.qq.com // 通过
http://www.qq.com.cn // 不通过
http://www.qq.com/a/b // 通过
http://www.qq.com?a=1 // 通过
http://www.123qq.com?a=1 // 不通过 使用下则的方法:1 ]+ x' z! `! w4 v! [- k v! l1 R
function check(url){
if(/\/\/w+\.qq\.com[^.]*$/.test(url)){
return true;
}else{
return false;
}
}
check('http://www.qq.com')
// true
check('http://www.qq.com.cn')
// false
check('http://www.qq.com/a/b')
// true
check('http://www.qq.com?a=1')
// true
check('http://www.123qq.com?a=1')
// false 这个正则很简单,包含 .qq.com 就可以,但是有一种情况,如果域名不是包含 qq.com 而仅仅是参数后面包含了 qq.com 怎么办?例如 http://www.baidu.com?redirect=http://www.qq.com/a: n/ k1 I0 P8 ]5 E: V
check('http://www.baidu.com?redirect=http://www.qq.com/a')
// true 如何排除这种情况?
0 n* O$ k/ `7 h2 C6 R5 hfunction check(url){
if(/^https?:\/\/w+\.qq\.com[^.]*$/.test(url)){
return true;
}else{
return false;
}
}
check('http://www.qq.com')
// true
check('http://www.qq.com.cn')
// false
check('http://www.qq.com/a/b')
// true
check('http://www.qq.com?a=1')
// true
check('http://www.123qq.com?a=1')
// false
check('http://www.baidu.com?redirect=http://www.qq.com/a')
// false
|