Skip to main content

How to get file extension using JavaScript and JQuery

Before uploading any files can be difficult as we have to manage those file using server side scripts.
There must be some ways to find the file type or file extension of uploaded files using JavaScript or JQuery.

It’s mandatory to find the file extension before we upload unwanted files to the server.

In this article we will see some different approaches to find out file extension using JavaScript or JQuery.

1) Using JavaScript string ‘replace’ function with Regular Expression.

Generally replace() function in JavaScript will replace given string in original string.
Syntax of Replace function

string.replace(searchString, newString).

Replace() function will accepts two parameters to replace one string into another string. In our case we will pass first parameter as a full file name and second parameter will be blank string.

Now we will use regular expression to get the filename till .(dot) in the filename and then we will replace it will blank string so only file extension will be remaining as a result.

Let’s see the example

var fileName = 'Tarun.jpg';
var extension = fileName.replace(/^.*\./, '');
console.log (extension);

Below is the output of the above code

I know regular expression is little bit confusing, so let’s see how it works. If we write “^.*” it means all the characters. If we write “\.” it means till it reaches the .(dot). If we put both expression together means get all the characters till it reaches the .(dot).
Put that both regular expression enclosed forward slash “/” one at the beginning and one at the end.
So full expression will be like below

/^.*\./

This was the first approach. Now let’s see at second approach

2) Using ‘substr()’ and ‘lastIndexOf()’ function of the JavaScript.

If we are using two JavaScript function together then also it will return file extension. ‘substr()’ function will extract specific value from a given string. ‘lastIndexOf()’ function will return lastIndexOf given character in the target string.

Let’s see the syntax of substr function

string.substr(startIndex, Length)

In the above syntax first parameter startIndex will be integer value which will indicate the starting point of the string and second parameter Length represents length to be extracted from the target string.

Now let’s see the syntax of the lastIndexOf function

string.lastIndexOf(searchString, startIndex);

In the above syntax first parameter searchString will be either character or string which we want to find from the target string and second parameter startIndex indicates from where we want to start in the target string.

Now let’s see the example to get the file extension

var fileName = 'Tarun.jpg';
var extension = fileName.substr((fileName.lastIndexOf('.') + 1));
console.log (extension);

Below is the output of the above code

3) Using ‘split’ and ‘pop’ function of the JavaScript

The JavaScript ‘split()’ function will split the string and returns an array of the words by separator.

Let’s see the syntax of the split function

string.split(separatorChar, limitOfSplitString);

In the above given syntax first parameter is the separatorChar will be used to split the string from that char and second parameter is the limitOfSplitString which will decides how many elements we want to retrieve.

The first parameter can be any character used to split the string and the second parameter is a numeric value defining the number of items returned as an arrays.

pop function will return the last element of an array. It can be only used with the array.

Now let’s split the filename with .(dot) and convert filename to an array, after that we will use pop function to get the last element of the array which is an extension.

var fileName = 'Tarun.jpg';
var extension = fileName.split('.').pop();
console.log (extension);

Below is the output of the above code

Conclusion

In the above all examples we have seen how to get file extension using regular expression and using various JavaScript function like split, pop, substr, lastIndexOf. If you have any query please write into the comment box or use contact us page.

See you in the next article.

Happy coding.

Tarun Dudhatra

Tarun is a writer of http://www.codingsharp.com website. He is a software professional with hands on 10+ years of experience. Generally he writes an article about microsoft technology, Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.