簡體   English   中英

網址中的FileName(不含查詢字符串)

[英]FileName from url excluding querystring

我有一個網址:

http://www.xyz.com/a/test.jsp?a=b&c=d

我如何獲得它的test.jsp

應該這樣做:

var path = document.location.pathname,
    file = path.substr(path.lastIndexOf('/'));

參考: document.locationsubstrlastIndexOf

我不會只告訴你答案,但是我會給你指導。 首先...去除“?”之后的所有內容 通過使用字符串實用程序和location.href.status(將為您提供查詢字符串)。 然后剩下的就是URL。 獲取最后一個“ /”之后的所有內容(提示:lastindexof)。

使用正則表達式。

var urlVal ='http://www.xyz.com/a/test.jsp?a=b&c=d'; var結果= /a\\/(.*)\\?/.exec(urlVal)[1]

regex返回一個數組,使用[1]獲得test.jsp

此方法不依賴於路徑名:

    <script>
        var url = 'http://www.xyz.com/a/test.jsp?a=b&c=d';
        var file_with_parameters = url.substr(url.lastIndexOf('/') + 1);
        var file = file_with_parameters.substr(0, file_with_parameters.lastIndexOf('?')); 
        // file now contains "test.jsp"
    </script>
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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM