如何从javascript中的文件夹路径获取最后一个文件夹名称? [英] How to get last folder name from folder path in javascript?
本文介绍了如何从javascript中的文件夹路径获取最后一个文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在javascript / jquery中,给出一个文件夹的路径,如:
In javascript/jquery, given a path to a folder like:
"http://www.blah/foo/bar/"
或
"http://www.blah/foo/bar" (this one doesn't have a slash in the end)
如何提取最后一个文件夹的名称?在这种情况下,它将是bar
。
How can you extract the name of the last folder? In this case it would be "bar"
.
是否有通过内置函数的简单方法?
Is there an easy way through a built in function?
谢谢。
推荐答案
使用正则表达式的强大功能:
Use the power of the regular expression :
var name = path.match(/([^\/]*)\/*$/)[1]
请注意,这不是最后一个文件夹(在您的情况下未定义,在URL的一般情况下)但最后一个路径令牌。
Notice that this isn't the last "folder" (which isn't defined in your case and in the general case of an URL) but the last path token.
这篇关于如何从javascript中的文件夹路径获取最后一个文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文