How to extract extension from file name string in javascript?

how to get the file extension of a file in a variable? for example, if I have a file like 1.txt I need a txt part.

+78
javascript
Mar 25 '09 at 10:00
source share
10 answers

An option that works with all of the following inputs:

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

:

 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
+213
Mar 25 '09 at 10:18
source share

Use the lastIndexOf method to search for the last period in a string and after that get the portion of the string:

 var ext = fileName.substr(fileName.lastIndexOf('.') + 1); 
+116
Mar 25 '09 at 10:06
source share

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

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

If the file name is not . , you will get the whole line.

<strong> 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' 
+85
Jan 14 '11 at 19:37
source share

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

 var myString = "this.is.my.file.txt" alert(myString.substring(myString.lastIndexOf(".")+1)) 
+17
Mar 25 '09 at 10:09
source share

It is better to use the following; It always works!

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

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

+10
Jul 18 '12 at 11:22
source share
 var x = "1.txt"; alert (x.substring(x.indexOf(".")+1)); 

note 1: this will not work if the file name is of the form file.example.txt
note 2: this will not succeed if the file name looks like a file

+1
Mar 25 '09 at 10:02
source share

This is a solution if there is more in your file. (dots) in the title.

 <script type="text/javascript">var x = "file1.asdf.txt"; var y = x.split("."); alert(y[(y.length)-1]);</script> 
+1
Mar 25 '09 at 10:06
source share

get the value in a variable and then split its extension just like that.

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

This will help you.

0
Sep 24 '11 at 9:05
source share

I am using the following code:

 var fileSplit = filename.split('.'); var fileExt = ''; if (fileSplit.length > 1) { fileExt = fileSplit[fileSplit.length - 1]; } return fileExt; 
-one
Mar 25 '09 at 10:06
source share

Try it. 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]; 

Hello

-one
Nov 16 '12 at 8:53
source share



All Articles