[英]From a url string, how can i get the file path without file name and param
var url = 'http ://domain.com/file.php?id=1'; var url = 'http://domain.com/file.php?id=1'; or或者
var url = 'https ://domain.com/file.php?id=1'; var url = 'https://domain.com/file.php?id=1'; or或者
var url = 'https ://domain.com'; var url = 'https://domain.com'; or或者
from either one of these urls I want to get only the path, like the one below:从这些网址之一我只想获取路径,如下所示:
var path = 'https ://domain.com'; var path = 'https://domain.com';
and lastly if the url is最后,如果网址是
var url = 'https ://domain.com/sg'; var url = 'https://domain.com/sg';
i should get我应该得到
var path = 'https ://domain.com/sg'; var path = 'https://domain.com/sg';
You can try this working example.你可以试试这个工作示例。 It should work in your case.它应该适用于您的情况。
https://jsfiddle.net/ch44Lmuk/10/ 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.