Reputation: 77
I have a script that grabs the filename from a URL but I have a problem.
Firstly, here's what I've got so far:
var img = $('img').attr('src'),
fileName_Index = img.lastIndexOf("/") + 1,
fileName = img.substr(fileName_Index);
If the URL was to have ?format=foo
after the filename, that part is added. Is there a way to remove the last part starting with the question mark leaving just the filename?
Upvotes: 2
Views: 7942
Reputation: 92893
Try adding this line to the end of your code sample:
fileName = fileName.replace(/[\#\?].*$/,''); // strips hashes, too
Upvotes: 2