Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一道腾讯手写题,如何判断 url 中只包含 qq.com #88

Open
sisterAn opened this issue Apr 19, 2021 · 2 comments
Open

一道腾讯手写题,如何判断 url 中只包含 qq.com #88

sisterAn opened this issue Apr 19, 2021 · 2 comments

Comments

@sisterAn
Copy link
Owner

例如:

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

解答:正则

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

check('http://www.baidu.com?redirect=http://www.qq.com/a')
// true

如何排除这种情况?

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')
// true

若有收获,就点个赞吧

@nooldey
Copy link

nooldey commented Apr 23, 2022

最后一个结果是false啊~

@RyanMeg123
Copy link

function check (url) { let reg = /^https?://\w+.qq.com[^.]*$/ return reg.test(url) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants