简体   繁体   English

如何从javascript中的文件夹路径获取最后一个文件夹名称?

[英]How to get last folder name from folder path in javascript?

In javascript/jquery, given a path to a folder like:在 javascript/jquery 中,给定一个文件夹的路径,如:

"http://www.blah/foo/bar/" 

or要么

"http://www.blah/foo/bar" (this one doesn't have a slash in the end)

How can you extract the name of the last folder?如何提取最后一个文件夹的名称? In this case it would be "bar" .在这种情况下,它将是"bar"

Is there an easy way through a built in function?有没有通过内置函数的简单方法?

Thanks.谢谢。

Use the power of the regular expression :使用正则表达式的强大功能:

var name = path.match(/([^\/]*)\/*$/)[1]

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.请注意,这不是最后一个“文件夹”(在您的情况和 URL 的一般情况下未定义),而是最后一个路径标记。

Use regular expressions!使用正则表达式! or:要么:

var arr = 'http://www.blah/foo/bar/'.split('/');
var last = arr[arr.length-1] || arr[arr.length-2];

this accounts for ' http://www.blah/foo/bar//// ' :p (or crashes the browser)这说明了“ http://www.blah/foo/bar//// ” :p (或使浏览器崩溃)

var arr = 'http://www.blah/foo/bar/////'.split('/');
var last = (function trololol(i) {
  return arr[i] || trololol(i-1);
})(arr.length-1);

Take a look at this:看看这个:

var lastFolder = location.href.split('/').filter(function(el) { 
    return el.trim().length > 0; 
}).pop();

 alert( location.href.split('/').filter(function(el) { return el.trim().length > 0; }).pop() ); alert( location.href ); var test = "http://www.blah/foo/bar/" ; alert( test.split('/').filter(function(el) { return el.trim().length > 0; }).pop() );

var myString = "http://www.blah/foo/bar";
var pathElements = myString.replace(/\/$/, '').split('/');
var lastFolder = pathElements[pathElements.length - 1];

This is pure JavaScript and doesn't need jQuery.这是纯 JavaScript,不需要 jQuery。

I usually use the combination of split and pop in javascript, because I usually get the folder addres from aws s3 , it's already clean:我通常在javascript中使用splitpop的组合,因为我通常从 aws s3 获取文件夹地址,它已经很干净了:

const teststring = 'image_1/ArtService/GB/ART-123/dependants/ASM-123';
const folder = teststring.split('/').pop();
console.log('folder:', folder);// ASM-123

You can split the string into a list and grab the last element in the list via a function.您可以将字符串拆分为列表并通过函数获取列表中的最后一个元素。

function checkString(stringToCheck) {
    var list1 = stringToCheck.split('/');
    if (list1[list1.length - 1].length < 1) {
        var item = list1[list1.length - 2]
    } else {
       var item = list1[list1.length - 1];
    }
    $('#results').append('Last Folder in '+stringToCheck+': <b>'+item+'</b><br>');

} }

From there you can find the last actual element.从那里您可以找到最后一个实际元素。

JSFiddle Example JSFiddle 示例

While it may not be the most elegant answer, it seems to be the easiest to understand for someone that might not know regular expressions.虽然它可能不是最优雅的答案,但对于可能不了解正则表达式的人来说,它似乎是最容易理解的。

Updated JSFiddle to display the results.更新了 JSFiddle 以显示结果。

var str = "...";
var segments = str.split("/");
var lastDir = (segments.length > 1) ? segments[segments.length - 2] : "";

First example yields "bar".第一个例子产生“bar”。 Second example yields "foo".第二个例子产生“foo”。

If you want to disregard trailing slash and consider the last segment as a folder as well, then a slight tweak to @bdesham's RegExp does the trick:如果您想忽略尾部斜杠并将最后一段也视为文件夹,那么对@bdesham 的 RegExp 稍作调整即可解决问题:

var segments = str.replace(/\/+$/, '').split('/');
var lastDir = (segments.length > 0) ? segments[segments.length - 1] : "";

A simple regexp can do the job一个简单的正则表达式就可以完成这项工作

var s = "toto1/toto2/toto3toto1/totofinale/";

s.replace(/^.*\/([^\/]+\/)$/, "$1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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