本文介绍了使用JavaScript从URL获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本可以从URL中获取文件名,但是我有问题.
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);
如果URL在文件名后具有?format=foo
,则添加该部分.有没有办法删除以问号开头的最后一部分而仅保留文件名?
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?
推荐答案
尝试将此行添加到代码示例的末尾:
Try adding this line to the end of your code sample:
fileName = fileName.replace(/[\#\?].*$/,''); // strips hashes, too
这篇关于使用JavaScript从URL获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!