开发者

Get extension from filename like variable - JavaScript - jQuery [duplicate]

This question already has answers here: 开发者_如何学运维 How can I get file extensions with JavaScript? (36 answers) Closed 9 years ago.

I have a varable var file = "testfile.txt"; I am using the following script to find its extension and it works well:

var parts = file.split('.');
var flename = parts[0];
var ext = parts[1];

But if i have a filename with more than one dots, the logic sucks.. It will give txt for filename.txt, but not in the case of file.nam.e.txt or something else.

Anybody suggest me the simplest solution to do this using JavaScript and/or jQuery?


Just so you get some alternatives, you can extract it with regular expressions too:

var full = "filename.ext.txt";

// Get all characters before the last dot
var filename = /.*(?=\.)/.exec(full);

// Get the part without dots on the end
var extension = /[^\.]*$/.exec(full);

// If there are no dots in the full name, filename will be empty and extension
// will contain the whole filename. Do some extra processing:
if(!filename){
    filename = full;
    extension = '';
}


var ext = 'hibernate.cfg.xml';

var index = ext.lastIndexOf('.');// .xml

alert(ext.substring(index+1)); // xml


var parts = file.split('.')
  , ext = (parts.length > 1 ? parts.pop() : null)
  , filename = parts.join('.');


Simple as that:

var ext = parts[parts.length - 1];

And as for the file name:

var flename = parts.slice(0, parts.length - 1).join(".");

Last but not least, I would also validate before that block of code that file is not empty to avoid exceptions.

Edit: another similar way that is more elegant:

var ext = parts.splice(-1).join();
var flename = parts.join(".");

Note that unlike the first approach, this will actually change the array parts so don't use it afterwards.


If file always contains at least one dot, you can use:

var ext = file.slice(file.lastIndexOf(".")+1);
var filename = file.slice(0,file.lastIndexOf("."));

And in a more general case, where file might contain no dot:

var ext = file.indexOf(".")<0?"":file.slice(file.lastIndexOf(".")+1);
var filename = file.indexOf(".")<0?file:file.slice(0,file.lastIndexOf("."));
And last, just for the sake of beautiful code:

var lastDotIndex = file.lastIndexOf(".");

if (lastDotIndex == -1) {    // Handles the case where there is no dot in "file"

    var fileExtension = "";
    var fileNameWithoutExtension = file;

} else {    // Common case 

    var fileExtension = file.slice(lastDotIndex+1);
    var fileNameWithoutExtension = file.slice(0,lastDotIndex);

}


You've already gotten numerous working solutions, but I thought I'd contribute with a object-oriented solution that might be useful for more advanced scenarios:

var File = function(fname){
    if (typeof fname === "undefined") {
        console.log("You need to pass a filename or path to file");
        return false;
    }
    // Correct slashes
    fname = fname.replace("\\", "/", fname);
    this.org = fname;
    if (fname.lastIndexOf("/")) {
        this.path = fname.substr(0,fname.lastIndexOf("/")+1);
        fname = fname.substr(fname.lastIndexOf("/")+1);
    }
    this.ext = fname.substr(fname.lastIndexOf(".")+1);
    this.name = fname.substr(0, fname.lastIndexOf("."));
}

Then you can do the following:

var file = new File("/full/path/to/file/foo.bar.baz");

Now, access the properties with file.name, file.ext and file.path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜