第 87 题:在输入框中如何判断输入的是一个正确的网址
yygmind opened this issue · 21 comments
yygmind commented
第 87 题:在输入框中如何判断输入的是一个正确的网址
lhyt commented
不上正则,一个简单的玩法
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 commented
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 commented
不上正则,一个简单的玩法
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 commented
不上正则,一个简单的玩法
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 commented
function isUrl(url) {
try {
new URL(url);
return true;
}catch(err){
return false;
}}ovarte commented
halionn commented
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 commented
/^(https?:\/\/)?([a-z0-9]\.|[a-z0-9][-a-z0-9]*[a-z0-9]\.)*([a-z]+)(:\d+)?(\/.*)?$/;wulichenyang commented
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 commented
/^(?:(https?):)?(?:\/\/)?([^\/?#:]+)?(?::(\d+))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$/CC712 commented
what is a URL from MDN
补个资料。
思路应该是对 URL的组成部分 分别进行校验
- protocol
- domain name
- port
- path (不用校验)
- params
- anchor (不用校验)
yaodongyi commented
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 commented
stackoverflow上的:
(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]
luotaiqiang commented
不上正则,一个简单的玩法
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 commented
lovetingyuan commented
const a = document.createElement('input')
a.type = 'url'
a.value = '..'
a.checkValidity() // false, 不推荐用于精确判断的场景
yuanxiang1990 commented
/^(http(s)?://)?([\w-]+.)?[\w-]+.[\w-]+(?[\w-]+=[\w-]+(&[\w-]+=[\w-_]+)*)?$/
flygo996 commented
serializedowen commented
Hydroism commented
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 commented
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))
这个代码看了我强迫症犯病

