在 Javascript 中需要一个 basename 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3820381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:16:59  来源:igfitidea点击:

Need a basename function in Javascript

javascriptstring

提问by PeterMmm

I need a short basename function (one-liner ?) for Javascript:

我需要一个用于 Javascript 的简短 basename 函数(单行?):

basename("/a/folder/file.a.ext") -> "file.a"
basename("/a/folder/file.ext") -> "file"
basename("/a/folder/file") -> "file"

That should strip the path and any extension.

那应该去掉路径和任何扩展名。

Update: For dot at the beginning would be nice to treat as "special" files

更新:对于开头的点,将其视为“特殊”文件会很好

basename("/a/folder/.file.a.ext") -> ".file.a"
basename("/a/folder/.file.ext") -> ".file"
basename("/a/folder/.file") -> ".file" # empty is Ok
basename("/a/folder/.fil") -> ".fil"  # empty is Ok
basename("/a/folder/.file..a..") -> # does'nt matter

回答by Tomo Huynh

function basename(path) {
   return path.split('/').reverse()[0];
}

Breaks up the path into component directories and filename then returns the last piece (the filename) which is the last element of the array.

将路径分解为组件目录和文件名,然后返回最后一部分(文件名),即数组的最后一个元素。

回答by Nivas

function baseName(str)
{
   var base = new String(str).substring(str.lastIndexOf('/') + 1); 
    if(base.lastIndexOf(".") != -1)       
        base = base.substring(0, base.lastIndexOf("."));
   return base;
}

If you can have both /and \as separators, you have to change the code to add one more line

如果可以同时使用/\作为分隔符,则必须更改代码以再添加一行

回答by Eugen Mihailescu

Any of the above works although they have no respect for speed/memory :-).

以上任何一种都有效,尽管它们不考虑速度/内存:-)。

A faster/simpler implementation should uses as fewer functions/operations as possible. RegExp is a bad choice because it consumes a lot of resources when actually we can achieve the same result but easier.

更快/更简单的实现应该使用尽可能少的函数/操作。RegExp 是一个糟糕的选择,因为它消耗了大量资源,而实际上我们可以实现相同的结果但更容易。

An implementation when you want the filename including extension (which in fact this is the genuine definitionof basename):

当您想要包含扩展名的文件名时的实现(实际上这是basename的真正定义):

function basename(str, sep) {
    return str.substr(str.lastIndexOf(sep) + 1);
}

If you need a custom basename implementationthat has to strip also the extension I would recommend instead a specific extension-stripping function for that case which you can call it whenever you like.

如果您需要一个自定义的basename 实现也必须剥离扩展名,我会推荐一个特定的扩展剥离函数,您可以随时调用它。

function strip_extension(str) {
    return str.substr(0,str.lastIndexOf('.'));
}

Usage example:

用法示例:

basename('file.txt','/'); // real basename
strip_extension(basename('file.txt','/')); // custom basename

They are separated such that you can combine them to obtain 3 different things: stripping the extention, getting the real-basename, getting your custom-basename. I regard it as a more elegant implementation than others approaches.

它们是分开的,因此您可以将它们组合起来以获得 3 种不同的东西:剥离扩展名、获取真实的基本名称、获取自定义基本名称。我认为它是比其他方法更优雅的实现。

回答by Randy

Maybe try to use existing packages if you can. http://nodejs.org/api/path.html

如果可以的话,也许尝试使用现有的包。 http://nodejs.org/api/path.html

var path = require('path');
var str = '/path/to/file/test.html'

var fileNameStringWithoutExtention = path.basename(str, '.html');
// returns 'test'

// let path determine the extension
var fileNameStringWithoutExtention = path.basename(str, path.extname(str));
// returns 'test'

Other examples:

其他例子:

var pathString = path.dirname(str);
var fileNameStringWithExtention = path.basename(str);
var fullPathAndFileNameString = path.join(pathString, fileNameString);

回答by user187291

 basename = function(path) {
    return path.replace(/.*\/|\.[^.]*$/g, '');
 }

replace anything that ends with a slash .*\/or dot - some non-dots - end \.[^.]*$with nothing

替换以斜线.*\/或点结尾的任何内容- 一些非点 -\.[^.]*$以任何内容结尾

回答by Arthur Araújo

Just like @3DFace has commented:

就像@3DFace 评论过的那样:

path.split(/[\/]/).pop(); // works with both separators

Or if you like prototypes:

或者,如果您喜欢原型:

String.prototype.basename = function(sep) {
  sep = sep || '\/';
  return this.split(new RegExp("["+sep+"]")).pop();
}

Testing:

测试:

var str = "http://stackoverflow.com/questions/3820381/need-a-basename-function-in-javascript";
alert(str.basename());

Will return "need-a-basename-function-in-javascript".

将返回“need-a-basename-function-in-javascript”。

Enjoy!

享受!

回答by Arthur Araújo

Another good solution:

另一个很好的解决方案:

function basename (path, suffix) {
  //  discuss at: http://locutus.io/php/basename/
  // original by: Kevin van Zonneveld (http://kvz.io)
  // improved by: Ash Searle (http://hexmen.com/blog/)
  // improved by: Lincoln Ramsay
  // improved by: djmix
  // improved by: Dmitry Gorelenkov
  //   example 1: basename('/www/site/home.htm', '.htm')
  //   returns 1: 'home'
  //   example 2: basename('ecra.php?p=1')
  //   returns 2: 'ecra.php?p=1'
  //   example 3: basename('/some/path/')
  //   returns 3: 'path'
  //   example 4: basename('/some/path_ext.ext/','.ext')
  //   returns 4: 'path_ext'

  var b = path
  var lastChar = b.charAt(b.length - 1)

  if (lastChar === '/' || lastChar === '\') {
    b = b.slice(0, -1)
  }

  b = b.replace(/^.*[\/\]/g, '')

  if (typeof suffix === 'string' && b.substr(b.length - suffix.length) === suffix) {
    b = b.substr(0, b.length - suffix.length)
  }

  return b
}

from: http://locutus.io/php/filesystem/basename/

来自:http: //locutus.io/php/filesystem/basename/

回答by IAM_AL_X

Contrary to misinformation provided above, regular expressions are extremely efficient. The caveat is that, when possible, they should be in a position so that they are compiled exactly once in the life of the program.

与上面提供的错误信息相反,正则表达式非常有效。需要注意的是,如果可能,它们应该处于一个位置,以便它们在程序的生命周期中只编译一次。

Here is a solution that gives both dirnameand basename.

这是一个同时提供dirnamebasename的解决方案。

const rx1 = /(.*)\/+([^/]*)$/;    // (dir/) (optional_file)
const rx2 = /()(.*)$/;            // ()     (file)

function dir_and_file(path) {
  // result is array with
  //   [0]  original string
  //   [1]  dirname
  //   [2]  filename
  return rx1.exec(path) || rx2.exec(path);
}
// Single purpose versions.
function dirname(path) {
  return (rx1.exec(path) || rx2.exec(path))[1];
}
function basename(path) {
  return (rx1.exec(path) || rx2.exec(path))[2];
}

As for performance, I have not measured it, but I expect this solution to be in the same range as the fastest of the others on this page, but this solution does more. Helping the real-world performance is the fact that rx1will match most actual paths, so rx2is rarely executed.

至于性能,我没有测量过,但我希望这个解决方案与本页其他解决方案中最快的解决方案在同一范围内,但这个解决方案做得更多。帮助现实世界的性能是rx1将匹配大多数实际路径的事实,因此rx2很少执行。

Here is some test code.

这是一些测试代码。

function show_dir(parts) {
  console.log("Original str :"+parts[0]);
  console.log("Directory nm :"+parts[1]);
  console.log("File nm      :"+parts[2]);
  console.log();
}
show_dir(dir_and_file('/absolute_dir/file.txt'));
show_dir(dir_and_file('relative_dir////file.txt'));
show_dir(dir_and_file('dir_no_file/'));
show_dir(dir_and_file('just_one_word'));
show_dir(dir_and_file('')); // empty string
show_dir(dir_and_file(null)); 

And here is what the test code yields:

这是测试代码产生的结果:

# Original str :/absolute_dir/file.txt
# Directory nm :/absolute_dir
# File nm      :file.txt
# 
# Original str :relative_dir////file.txt
# Directory nm :relative_dir
# File nm      :file.txt
# 
# Original str :dir_no_file/
# Directory nm :dir_no_file
# File nm      :
# 
# Original str :just_one_word
# Directory nm :
# File nm      :just_one_word
# 
# Original str :
# Directory nm :
# File nm      :
# 
# Original str :null
# Directory nm :
# File nm      :null

By the way, "node" has a built in module called "path" that has "dirname" and "basename". Node's "path.dirname()" function accurately imitates the behavior of the "bash" shell's "dirname," but is that good? Here's what it does:

顺便说一下,“node”有一个名为“path”的内置模块,它有“dirname”和“basename”。Node 的“path.dirname()”函数准确地模仿了“bash”shell 的“dirname”的行为,但是这样好吗?这是它的作用:

  1. Produces '.'(dot) when path==""(empty string).
  2. Produces '.'(dot) when path=="just_one_word".
  3. Produces '.'(dot) when path=="dir_no_file/".
  1. '.'path==""(空字符串)时产生(点)。
  2. 时产生'.'(点)path=="just_one_word"
  3. 时产生'.'(点)path=="dir_no_file/"

I prefer the results of the function defined above.

我更喜欢上面定义的函数的结果。

回答by DOCTYPE HTML

This is my implementation which I use in my fundamental js file.

这是我在我的基本 js 文件中使用的实现。

// BASENAME

Window.basename = function() {
    var basename = window.location.pathname.split(/[\/]/);
    return basename.pop() || basename.pop();
}

回答by Chetabahana

JavaScript Functions for basenameand also dirname:

basenamedirname 的JavaScript 函数:

function basename(path) {
     return path.replace(/.*\//, '');
}

function dirname(path) {
     return path.match(/.*\//);
}

Sample:

样本:

Input                       dirname()           basename()
/folder/subfolder/file.ext  /folder/subfolder/  file.ext
/folder/subfolder           /folder/            subfolder
/file.ext                   file.ext            /
file.ext                    file.ext            null

See reference.

参考