如何使用javascript检查URL是否只包含域?

如何使用javascript检查URL是否只包含域?,javascript,string,url,Javascript,String,Url,如何检查url是否只包含javascript中的域 let s = 'some url string'; 那一定像下面那样工作 https://google.com/ --> true https://docs.google.com/ --> true https://google.com/blabla --> false https://google.com/blabla/ ---> false https://docs.google.com/blabla/ --

如何检查url是否只包含javascript中的域

let s = 'some url string';
那一定像下面那样工作

https://google.com/ --> true
https://docs.google.com/ --> true

https://google.com/blabla  --> false
https://google.com/blabla/ ---> false
https://docs.google.com/blabla/ ---> false

您可以使用
Window.location
获取所有这些详细信息

例:关于这个问题:

window.location.hostname  ==>> // "stackoverflow.com"

window.location.pathname == >> ///questions/53249750/how-to-check-whether-url-only-contains-the-domain-in-js

window.location.href == >>
"https://stackoverflow.com/questions/53249750/how-to-check-whether-url-only- 
 contains-the-domain-in-js"
您可以检查路径名并执行您的操作:

if (window.location.pathname === "" || window.location.pathname === "/") {
   return true
}

您可以使用
Window.location
获取所有这些详细信息

例:关于这个问题:

window.location.hostname  ==>> // "stackoverflow.com"

window.location.pathname == >> ///questions/53249750/how-to-check-whether-url-only-contains-the-domain-in-js

window.location.href == >>
"https://stackoverflow.com/questions/53249750/how-to-check-whether-url-only- 
 contains-the-domain-in-js"
您可以检查路径名并执行您的操作:

if (window.location.pathname === "" || window.location.pathname === "/") {
   return true
}

您可以使用全局
URL

const url = new URL('', 'https://google.com/blabla ');
console.log(url.hostname); // "google.com"
console.log(url.pathname); // "/blabla" 
您可以检查
url.pathname
,如果没有路径名,它将返回
/

const url = new URL('', 'https://google.com ');
console.log(url.hostname); // "google.com"
console.log(url.pathname); // "/"

您可以使用全局
URL

const url = new URL('', 'https://google.com/blabla ');
console.log(url.hostname); // "google.com"
console.log(url.pathname); // "/blabla" 
您可以检查
url.pathname
,如果没有路径名,它将返回
/

const url = new URL('', 'https://google.com ');
console.log(url.hostname); // "google.com"
console.log(url.pathname); // "/"

您可以使用正则表达式检查URL内容。
/^https?:\/\/[^\/?]+\/$/g
匹配任何以
http
开头、以域后缀和
/
结尾的URL

var url = 'https://google.com/';
/^https?:\/\/[^\/?]+\/$/g.test(url) // true
函数testURL(url){
返回/^https?:\/\/[^\/?]+\/$/g.test(url);
}
log(testURL('https://google.com/'));
log(testURL('https://docs.google.com/'));

log(testURL('https://google.com/blabla')); 您可以使用正则表达式检查URL内容。
/^https?:\/\/[^\/?]+\/$/g
匹配任何以
http
开头、以域后缀和
/
结尾的URL

var url = 'https://google.com/';
/^https?:\/\/[^\/?]+\/$/g.test(url) // true
函数testURL(url){
返回/^https?:\/\/[^\/?]+\/$/g.test(url);
}
log(testURL('https://google.com/'));
log(testURL('https://docs.google.com/'));

log(testURL('https://google.com/blabla')); 使用Window.location.href在那里你将获得完整的url作为数组(可能是),然后你可以拆分它并检查需要检查的内容。你能给我举个例子吗?
“http://google.com“.includes(“google”)//true
否,除google.composible duplicate of Use Window.location.href外的任何url都将以数组形式获取完整url(可能是)然后你可以拆分它并检查需要检查的内容。你能给我举个例子吗?
“http://google.com.includes(“谷歌”)//true
no,任何非google.composible副本的url注意:这假设OP要检查的字符串基于当前位置而不是某个变量输入注意:这假设OP要检查的字符串基于当前位置而不是某个变量输入注意:
url
不受internet explorer支持。注意internet explorer不支持
URL