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?
Simple as that:
And as for the file name:
Last but not least, I would also validate before that block of code that
fileis not empty to avoid exceptions.Edit: another similar way that is more elegant:
Note that unlike the first approach, this will actually change the array
partsso don’t use it afterwards.