qappleh / Interview

我是追梦赤子心,公众号「深圳湾码农」的作者,某上市集团公司高级前端开发,深耕前端领域多年,每天攻破一道题,带你从0到1系统构建web全栈完整的知识体系!
https://github.com/qappleh/Interview
1.14k stars 95 forks source link

Day367:如何判断url中只包含qq.com? #370

Open qappleh opened 3 years ago

qappleh commented 3 years ago

例如:

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 // 不通过

qappleh commented 3 years ago
function 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