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

第 87 题:在输入框中如何判断输入的是一个正确的网址 #138

Open
yygmind opened this issue Jun 11, 2019 · 16 comments
Open

第 87 题:在输入框中如何判断输入的是一个正确的网址 #138

yygmind opened this issue Jun 11, 2019 · 16 comments
Labels

Comments

@yygmind
Copy link
Contributor

@yygmind yygmind commented Jun 11, 2019

No description provided.

@lhyt

This comment has been minimized.

Copy link

@lhyt lhyt commented Jun 11, 2019

不上正则,一个简单的玩法

function isUrl(url) {
	const a = document.createElement('a')
	a.href = url
	return [
		/^(http|https):$/.test(a.protocol),
		a.host,
		a.pathname !== url,
		a.pathname !== `/${url}`,
	].find(x => !x) === undefined
}
@andyliangshan

This comment has been minimized.

Copy link

@andyliangshan andyliangshan commented Jun 12, 2019

var reg=/^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-]+).)+([A-Za-z0-9-/])+$/;
reg.test(document.querySelector('input').value);

@IdeaEcho

This comment has been minimized.

Copy link

@IdeaEcho IdeaEcho commented Jun 14, 2019

不上正则,一个简单的玩法

function isUrl(url) {
	const a = document.createElement('a')
	a.href = url
	return [
		/^(http|https):$/.test(a.protocol),
		a.host,
		a.pathname !== url,
		a.pathname !== `/${url}`,
	].find(x => !x) === undefined
}

可以问一下吗?a.pathname !== /${url} 这个没理解

@lhyt

This comment has been minimized.

Copy link

@lhyt lhyt commented Jun 14, 2019

不上正则,一个简单的玩法

function isUrl(url) {
	const a = document.createElement('a')
	a.href = url
	return [
		/^(http|https):$/.test(a.protocol),
		a.host,
		a.pathname !== url,
		a.pathname !== `/${url}`,
	].find(x => !x) === undefined
}

可以问一下吗?a.pathname !== /${url} 这个没理解

输入/a 和 输入 a,不算完整网站。但是pathname都是/a

@chenshengshui

This comment has been minimized.

Copy link

@chenshengshui chenshengshui commented Jun 14, 2019

function isUrl(url) {
       try {
           new URL(url);
           return true;
       }catch(err){
     return false;
}}
@ovarte

This comment has been minimized.

Copy link

@ovarte ovarte commented Jun 15, 2019

function isUrl(url) {
       try {
           new URL(url);
           return true;
       }catch(err){
     return false;
}}

image

@halionn

This comment has been minimized.

Copy link

@halionn halionn commented Jun 28, 2019

const isUrl = urlStr => {
    try {
        const { href, origin, host, hostname, pathname } = new URL(urlStr)
        return href && origin && host && hostname && pathname && true
    } catch (e) {
        return false
    }
}
@SuAgnes

This comment has been minimized.

Copy link

@SuAgnes SuAgnes commented Aug 2, 2019

 /^(https?:\/\/)?([a-z0-9]\.|[a-z0-9][-a-z0-9]*[a-z0-9]\.)*([a-z]+)(:\d+)?(\/.*)?$/;
@wulichenyang

This comment has been minimized.

Copy link

@wulichenyang wulichenyang commented Aug 5, 2019

const isUrl = urlStr => {
    try {
        const { href, origin, host, hostname, pathname } = new URL(urlStr)
        return href && origin && host && hostname && pathname && true
    } catch (e) {
        return false
    }
}

可能还需要检查一下protocol是http|https

@yuwanli

This comment has been minimized.

Copy link

@yuwanli yuwanli commented Aug 31, 2019

/^(?:(https?):)?(?:\/\/)?([^\/?#:]+)?(?::(\d+))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$/
@CC712

This comment has been minimized.

Copy link

@CC712 CC712 commented Sep 10, 2019

what is a URL from MDN
补个资料。
思路应该是对 URL的组成部分 分别进行校验

  • protocol
  • domain name
  • port
  • path (不用校验)
  • params
  • anchor (不用校验)
@yaodongyi

This comment has been minimized.

Copy link

@yaodongyi yaodongyi commented Sep 17, 2019

let url = 'https://www.baidu.com';
function searchUrl(url) {
    try {
        if (new URL(url) && (new URL(url).protocol === "http:" || new URL(url).protocol === "https:") && url.match(new RegExp(new URL(url).protocol + "//")).index === 0) return true
    } catch (err) {
        console.log("不是一个正确的网址");
    }
};
console.log(searchUrl(url))
@chenyueyun

This comment has been minimized.

Copy link

@chenyueyun chenyueyun commented Sep 18, 2019

stackoverflow上的:

(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]

@yygmind yygmind added the 编程题 label Dec 16, 2019
@luotaiqiang

This comment has been minimized.

Copy link

@luotaiqiang luotaiqiang commented Dec 18, 2019

不上正则,一个简单的玩法

function isUrl(url) {
	const a = document.createElement('a')
	a.href = url
	return [
		/^(http|https):$/.test(a.protocol),
		a.host,
		a.pathname !== url,
		a.pathname !== `/${url}`,
	].find(x => !x) === undefined
}

@lhyt
isUrl("ww......") //true测试一下就不对

@NARUTOne

This comment has been minimized.

Copy link

@NARUTOne NARUTOne commented Dec 20, 2019

function isUrl(url) {
       try {
           new URL(url);
           return true;
       }catch(err){
     return false;
}}

image

URL() IE下不兼容

@lovetingyuan

This comment has been minimized.

Copy link

@lovetingyuan lovetingyuan commented Mar 27, 2020

const a = document.createElement('input')
a.type = 'url'
a.value = '..'
a.checkValidity() // false, 

不推荐用于精确判断的场景

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

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.