Need a basename function in Javascript

后端 未结 19 1949
野性不改
野性不改 2020-11-29 02:44

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

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


        
相关标签:
19条回答
  • 2020-11-29 03:11

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

    // BASENAME
    
    Window.basename = function() {
        var basename = window.location.pathname.split(/[\\/]/);
        return basename.pop() || basename.pop();
    }
    
    0 讨论(0)
  • 2020-11-29 03:11

    JavaScript Functions for basename and also dirname:

    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.

    0 讨论(0)
  • 2020-11-29 03:12

    Fast without regular expressions, suitable for both path types '/' and '\'. Gets the extension also:

    function baseName(str)
    {
        let li = Math.max(str.lastIndexOf('/'), str.lastIndexOf('\\'));
        return new String(str).substring(li + 1);
    }
    
    0 讨论(0)
  • 2020-11-29 03:13

    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/

    0 讨论(0)
  • 2020-11-29 03:15
    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.

    0 讨论(0)
  • 2020-11-29 03:18

    Maybe try to use existing packages if you can. 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);
    
    0 讨论(0)
提交回复
热议问题