假设我们的网站可以有如下URL:
http://domainame.com/dir/one-simple-name.html
https://dmainame.com/mail/send.php
https://dmainame.com/mail/read
所以我希望能够检索到相关信息。
dir/one-simple-name
mail/send
mail/read
最佳实践是什么?
假设我们的网站可以有如下URL:
http://domainame.com/dir/one-simple-name.html
https://dmainame.com/mail/send.php
https://dmainame.com/mail/read
dir/one-simple-name
mail/send
mail/read
最佳实践是什么?
大家退后!我会正则表达式!试试这个:
var my_location = window.location.toString().match(/\/\/[^\/]+\/([^\.]+)/)[1];
,
var url = "https://www.somegoodwebsite.com/happy/ending/results/index.html";
console.log(url.replace(/^.*\/\/[^\/]+/, '') );
;
Result:
happy/ending/results/index.html
var result = window.location.pathname.replace(/\.[^\.\/]+$/, "").substr(1);
window.location.href - the entire URL
window.location.host - the hostname and port number - [www.sample.com]:80
window.location.hostname - the hostname - www.sample.com
window.location.pathname - just the path part - /search
window.location.search - the part of the URL after the ? symbol - ?q=demo
window.location.pathname
,如果您需要从文件名中去掉文件扩展名,则可以使用一些额外的javascript来完成:var result = window.location.pathname.replace(/\.[^\.\/]+$/, "").substr(1);
""
替换任何文件扩展名(有效地删除它),并删除前导斜杠。var path = $('<a>', {href: 'http://google.com/foo/bar.py'})[0].pathname.replace(/\.(.+)$/,'')
.
拆分字符串。 - Brad