How to extract extension from filename string in Javascript?

Javascript

Javascript Problem Overview


how would i get the File extension of the file in a variable? like if I have a file as 1.txt I need the txt part of it.

Javascript Solutions


Solution 1 - Javascript

A variant that works with all of the following inputs:

  • "file.name.with.dots.txt"
  • "file.txt"
  • "file"
  • ""
  • null
  • undefined

would be:

var re = /(?:\.([^.]+))?$/;

var ext = re.exec("file.name.with.dots.txt")[1];   // "txt"
var ext = re.exec("file.txt")[1];                  // "txt"
var ext = re.exec("file")[1];                      // undefined
var ext = re.exec("")[1];                          // undefined
var ext = re.exec(null)[1];                        // undefined
var ext = re.exec(undefined)[1];                   // undefined

Explanation

(?:         # begin non-capturing group
.        #   a dot
(         #   begin capturing group (captures the actual extension)
[^.]+   #     anything except a dot, multiple times
)         #   end capturing group
)?          # end non-capturing group, make it optional
$           # anchor to the end of the string

Solution 2 - Javascript

I personally prefer to split the string by . and just return the last array element :)

var fileExt = filename.split('.').pop();

If there is no . in filename you get the entire string back.

Examples:

'some_value'                                   => 'some_value'
'.htaccess'                                    => 'htaccess'
'../images/something.cool.jpg'                 => 'jpg'
'http://www.w3schools.com/jsref/jsref_pop.asp' => 'asp'
'http://stackoverflow.com/questions/680929'    => 'com/questions/680929'

Solution 3 - Javascript

Use the lastIndexOf method to find the last period in the string, and get the part of the string after that:

var ext = fileName.substr(fileName.lastIndexOf('.') + 1);

Solution 4 - Javascript

I would recommend using lastIndexOf() as opposed to indexOf()

var myString = "this.is.my.file.txt"
alert(myString.substring(myString.lastIndexOf(".")+1))

Solution 5 - Javascript

Better to use the following; Works always!

var ext =  fileName.split('.').pop();

This will return the extension without a dot prefix. You can add "." + ext to check against the extensions you wish to support!

Solution 6 - Javascript

var x = "1.txt";
alert (x.substring(x.indexOf(".")+1));

note 1: this will not work if the filename is of the form file.example.txt
note 2: this will fail if the filename is of the form file

Solution 7 - Javascript

Try this. May solve your problem.

var file_name_string = "file.name.string.png"

var file_name_array = file_name_string.split(".");
var file_extension = file_name_array[file_name_array.length - 1];

Regards

Solution 8 - Javascript

This is the solution if your file has more . (dots) in the name.

<script type="text/javascript">var x = "file1.asdf.txt";
var y = x.split(".");
alert(y[(y.length)-1]);</script>

Solution 9 - Javascript

I use code below:

var fileSplit = filename.split('.');
var fileExt = '';
if (fileSplit.length > 1) {
fileExt = fileSplit[fileSplit.length - 1];
} 
return fileExt;

Solution 10 - Javascript

get the value in the variable & then separate its extension just like this.

var find_file_ext=document.getElementById('filename').value;
var file_ext=/[^.]+$/.exec(find_file_ext); 

This will help you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsantanuView Question on Stackoverflow
Solution 1 - JavascriptTomalakView Answer on Stackoverflow
Solution 2 - JavascriptRobert MullaneyView Answer on Stackoverflow
Solution 3 - JavascriptGuffaView Answer on Stackoverflow
Solution 4 - JavascriptRuss CamView Answer on Stackoverflow
Solution 5 - JavascriptAfzal AliView Answer on Stackoverflow
Solution 6 - JavascriptcherouvimView Answer on Stackoverflow
Solution 7 - JavascriptVladimir DjuricicView Answer on Stackoverflow
Solution 8 - JavascriptBogdan ConstantinescuView Answer on Stackoverflow
Solution 9 - JavascriptPragmainlineView Answer on Stackoverflow
Solution 10 - JavascriptRavinder SinghView Answer on Stackoverflow