简体   繁体   中英

FileName from url excluding querystring

I have a url :

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

How do I get test.jsp of it ?

This should do it:

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

Reference: document.location , substr , lastIndexOf

I wont just show you the answer, but I'll give you direction to it. First... strip out everything after the "?" by using a string utility and location.href.status (that will give you the querystring). Then what you will be left with will be the URL; get everything after the last "/" (hint: lastindexof).

Use a regular expression.

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

the regex returns an array, use [1] to get the test.jsp

This method does not depend on pathname:

    <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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM