var url =' http://domain.com/file.php?id = 1&#39 ;; 或
var url =' https://domain.com/file.php?id = 1&#39 ;; 或
var url =' https://domain.com' ;; 或
从这些网址中的任何一个我只想获得路径,如下所示:
var path =' https://domain.com' ;;
最后如果网址是
var url =' https://domain.com/sg' ;;
我应该
var path =' https://domain.com/sg' ;;
答案 0 :(得分:0)
您可以尝试这个有效的例子。它应该适用于你的情况。
https://jsfiddle.net/ch44Lmuk/10/
function getnewUrl(url) {
url = url.replace("http://", "");
var index = url.indexOf('/');
if (index == -1) return url;
else {
var split = url.split('/')
var domain = split[0];
if (split[1].length == 0) return domain;
url = url.slice(index);
index = url.lastIndexOf('.');
if (index != -1) {
index = url.lastIndexOf('/');
url = url.slice(0, index);
} else {
index = url.lastIndexOf('/');
if (index == url.length - 1) url = url.slice(0, index);
}
return domain + url;
}
}