How to trim a file extension from a String in JavaScript?

前端 未结 23 1899
醉酒成梦
醉酒成梦 2020-11-30 17:21

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let\'s assume the file nam

相关标签:
23条回答
  • 2020-11-30 17:29

    This is where regular expressions come in handy! Javascript's .replace() method will take a regular expression, and you can utilize that to accomplish what you want:

    // assuming var x = filename.jpg or some extension
    x = x.replace(/(.*)\.[^.]+$/, "$1");
    
    0 讨论(0)
  • 2020-11-30 17:29

    Another one liner - we presume our file is a jpg picture >> ex: var yourStr = 'test.jpg';

        yourStr = yourStr.slice(0, -4); // 'test'
    
    0 讨论(0)
  • 2020-11-30 17:29

    You can use path to maneuver.

    var MYPATH = '/User/HELLO/WORLD/FILENAME.js';
    var MYEXT = '.js';
    var fileName = path.basename(MYPATH, MYEXT);
    var filePath = path.dirname(MYPATH) + '/' + fileName;
    

    Output

    > filePath
    '/User/HELLO/WORLD/FILENAME'
    > fileName
    'FILENAME'
    > MYPATH
    '/User/HELLO/WORLD/FILENAME.js'
    
    0 讨论(0)
  • 2020-11-30 17:30

    If you have to process a variable that contains the complete path (ex.: thePath = "http://stackoverflow.com/directory/subdirectory/filename.jpg") and you want to return just "filename" you can use:

    theName = thePath.split("/").slice(-1).join().split(".").shift();
    

    the result will be theName == "filename";

    To try it write the following command into the console window of your chrome debugger: window.location.pathname.split("/").slice(-1).join().split(".").shift()

    If you have to process just the file name and its extension (ex.: theNameWithExt = "filename.jpg"):

    theName = theNameWithExt.split(".").shift();
    

    the result will be theName == "filename", the same as above;

    Notes:

    1. The first one is a little bit slower cause performes more operations; but works in both cases, in other words it can extract the file name without extension from a given string that contains a path or a file name with ex. While the second works only if the given variable contains a filename with ext like filename.ext but is a little bit quicker.
    2. Both solutions work for both local and server files;

    But I can't say nothing about neither performances comparison with other answers nor for browser or OS compatibility.

    working snippet 1: the complete path

    var thePath = "http://stackoverflow.com/directory/subdirectory/filename.jpg";
    theName = thePath.split("/").slice(-1).join().split(".").shift();
    alert(theName);
      

    working snippet 2: the file name with extension

    var theNameWithExt = "filename.jpg";
    theName = theNameWithExt.split("/").slice(-1).join().split(".").shift();
    alert(theName);
      

    working snippet 2: the file name with double extension

    var theNameWithExt = "filename.tar.gz";
    theName = theNameWithExt.split("/").slice(-1).join().split(".").shift();
    alert(theName);
      

    0 讨论(0)
  • 2020-11-30 17:30

    This is the code I use to remove the extension from a filename, without using either regex or indexOf (indexOf is not supported in IE8). It assumes that the extension is any text after the last '.' character.

    It works for:

    • files without an extension: "myletter"
    • files with '.' in the name: "my.letter.txt"
    • unknown length of file extension: "my.letter.html"

    Here's the code:

    var filename = "my.letter.txt" // some filename
    
    var substrings = filename.split('.'); // split the string at '.'
    if (substrings.length == 1)
    {
      return filename; // there was no file extension, file was something like 'myfile'
    }
    else
    {
      var ext = substrings.pop(); // remove the last element
      var name = substrings.join(""); // rejoin the remaining elements without separator
      name = ([name, ext]).join("."); // readd the extension
      return name;
    }
    
    0 讨论(0)
  • 2020-11-30 17:31

    I don't know if it's a valid option but I use this:

    name = filename.split(".");
    // trimming with pop()
    name.pop();
    // getting the name with join()
    name.join('.'); // we split by '.' and we join by '.' to restore other eventual points.
    

    It's not just one operation I know, but at least it should always work!

    UPDATE: If you want a oneliner, here you are:

    (name.split('.').slice(0, -1)).join('.')

    0 讨论(0)
提交回复
热议问题