使用Javascript从路径中获取文件名 [英] Getting just the filename from a path with Javascript
本文介绍了使用Javascript从路径中获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个完整的图像路径,我使用jquery来读取,格式为 $('img.my_image')。attr('src')
但是我只想要文件名部分(丢弃路径)。
I have a full path to an image, which I am using jquery to read, in the form $('img.my_image').attr('src')
however I just want the filename portion (discarding the path).
是否有任何内置函数可以执行此操作,还是正则表达式是唯一的选项?
Are there any built-in functions to do this, or would a regex be the only option?
推荐答案
var fileNameIndex = yourstring.lastIndexOf("/") + 1;
var filename = yourstring.substr(fileNameIndex);
这篇关于使用Javascript从路径中获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文