来自url的FileName不包括查询字符串
我有一个url:
http://www.xyz.com/a/test.jsp?a=b&c=d
我如何获得它的test.jsp
?
这应该这样做:
var path = document.location.pathname, file = path.substr(path.lastIndexOf('/'));
参考: document.location
, substr
, lastIndexOf
我不会告诉你答案,但我会给你指路。 首先……在“?”之后删除所有内容。 通过使用字符串实用程序和location.href.status(它将为您提供查询字符串)。 那么你将留下的是URL; 在最后一个“/”之后获取所有内容(提示:lastindexof)。
使用正则表达式。
var urlVal =’http://www.xyz.com/a/test.jsp?a = b&c = d’; var result = /a\/(.*)\?/.exec(urlVal)[1]
正则表达式返回一个数组,使用[1]来获取test.jsp
此方法不依赖于路径名:
var your_link = "http://www.xyz.com/a/test.jsp?a=b&c=d"; // strip the query from the link your_link = your_link.split("?"); your_link = your_link[0]; // get the the test.jsp or whatever is there var the_part_you_want = your_link.substring(your_link.lastIndexOf("/")+1);
试试这个:
/\/([^/]+)$/.exec(window.location.pathname)[1]