117
rated 0 times
[
122]
[
5]
/ answers: 1 / hits: 74357
/ 13 Years ago, wed, january 25, 2012, 12:00:00
I'm sure this must be easy enough, but I'm struggling...
var regexFileName = /[^\]*$/; // match filename
var regexFileExtension = /(w+)$/; // match file extension
function displayUpload() {
var path = $el.val(); //This is a file input
var filename = path.match(regexFileName); // returns file name
var extension = filename[0].match(regexFileExtension); // returns extension
console.log(The filename is + filename[0]);
console.log(The extension is + extension[0]);
}
The function above works fine, but I'm sure it must be possible to achieve with a single regex, by referencing different parts of the array returned with the .match() method. I've tried combining these regex but without success.
Also, I'm not using a string to test it on in the example, as console.log() escapes the backslashes in a filepath and it was starting to confuse me :)
More From » jquery