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 · 21 comments
Open

Comments

@yygmind
Copy link
Contributor

yygmind commented Jun 11, 2019

No description provided.

@lhyt
Copy link

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
Copy link

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
Copy link

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

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
Copy link

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
Copy link

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

@ovarte
Copy link

ovarte commented Jun 15, 2019

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

image

@halionn
Copy link

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
Copy link

SuAgnes commented Aug 2, 2019

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

@wulichenyang
Copy link

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
Copy link

yuwanli commented Aug 31, 2019

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

@CC712
Copy link

CC712 commented Sep 10, 2019

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

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

@yaodongyi
Copy link

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
Copy link

stackoverflow上的:

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

@luotaiqiang
Copy link

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
Copy link

NARUTOne commented Dec 20, 2019

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

image

URL() IE下不兼容

@lovetingyuan
Copy link

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

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

@yuanxiang1990
Copy link

/^(http(s)?://)?([\w-]+.)?[\w-]+.[\w-]+(?[\w-]+=[\w-]+(&[\w-]+=[\w-_]+)*)?$/

@flygo996
Copy link

image

@serializedowen
Copy link

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

image

这个本来就是合法url

@Hydroism
Copy link

function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

vue-element-admin 上面的方法

@yeyuguo
Copy link

yeyuguo commented Jan 22, 2021

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))

这个代码看了我强迫症犯病

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

No branches or pull requests