How can I get file extensions with JavaScript?
Newer Edit: Lots of things have changed since this question was initially posted - there's a lot of really good information in wallacer's revised answer as well as VisioN's excellent breakdown
Edit: Just because this is the accepted answer; wallacer's answer is indeed much better:
return filename.split('.').pop();
My old answer:
return /[^.]+$/.exec(filename);
Should do it.
Edit: In response to PhiLho's comment, use something like:
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
Get the file extension in the JavaScript
You can split the name file by dot, and access last index:
function sendFunc_web() {
var inp = document.getElementById('vasplus_multiple_files');
var count = inp.files.length;
for (var i = 0; i < inp.files.length; ++i) {
var num = i + 1;
var name = inp.files.item(i).name;
console.log(name)
let ext = ext[ext.length - 1]
console.log(ext)
}
}
Node.js get file extension
I believe you can do the following to get the extension of a file name.
var path = require('path')
path.extname('index.html')
// returns
'.html'
If you would like to get all extensions in a file name (e.g. filename.css.gz
=> css.gz
), try this:
const ext = 'filename.css.gz'
.split('.')
.filter(Boolean) // removes empty extensions (e.g. `filename...txt`)
.slice(1)
.join('.')
console.log(ext) // prints 'css.gz'
javascript - get the filename and extension from input type=file
Use lastIndexOf to get the last \
as an index and use substr to get the remaining string starting from the last index of \
function getFile(filePath) { return filePath.substr(filePath.lastIndexOf('\\') + 1).split('.')[0]; }
function getoutput() { outputfile.value = getFile(inputfile.value); extension.value = inputfile.value.split('.')[1]; }
<input id='inputfile' type='file' name='inputfile' onChange='getoutput()'><br> Output Filename <input id='outputfile' type='text' name='outputfile'><br> Extension <input id='extension' type='text' name='extension'>
How can I get file extensions with JavaScript?
Newer Edit: Lots of things have changed since this question was initially posted - there's a lot of really good information in wallacer's revised answer as well as VisioN's excellent breakdown
Edit: Just because this is the accepted answer; wallacer's answer is indeed much better:
return filename.split('.').pop();
My old answer:
return /[^.]+$/.exec(filename);
Should do it.
Edit: In response to PhiLho's comment, use something like:
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
Extract filename and extension in js
You can use the split()
function combined with pop()
to get the file full name (name + extension) and detect the separation between your file name and extension with lastIndexOf()
:
var str = '/vagrant/modules/americabuy/tranzilaNotifications/class/../../../a_tranzila/log/tranzila_2018-01-09_08-58-47.json';
function fileNameAndExt(str){ var file = str.split('/').pop(); return [file.substr(0,file.lastIndexOf('.')),file.substr(file.lastIndexOf('.')+1,file.length)]}
console.log(fileNameAndExt(str));
How to trim a file extension from a String in JavaScript?
If you know the length of the extension, you can use x.slice(0, -4)
(where 4 is the three characters of the extension and the dot).
If you don't know the length @John Hartsock regex would be the right approach.
If you'd rather not use regular expressions, you can try this (less performant):
filename.split('.').slice(0, -1).join('.')
Note that it will fail on files without extension.
Related Topics
Sort Array by Firstname (Alphabetically) in JavaScript
How to Prevent Enter Keypress to Submit a Web Form
Facebook Social Plug-In Not Showing Up When Added Dynamically
Browsers, Time Zones, Chrome 67 Error (Historic Timezone Changes)
Reactjs - Does Render Get Called Any Time "Setstate" Is Called
Required Field Validations Not Working in Jquery Popup MVC 4
What Do These JavaScript Bitwise Operators Do
What Is Typescript and Why Would I Use It in Place of JavaScript
Navigator.Geolocation.Getcurrentposition Sometimes Works Sometimes Doesn'T
How to Get Node Datum on Mouseover in D3 V6
How to Fix Error: Listen Eaddrinuse While Using Nodejs
Differencebetween String Primitives and String Objects in JavaScript
Defining a JavaScript Prototype
How to Pass Data from a Page to Another Page Using React Router
Javascript/Jquery: $(Window).Resize How to Fire After the Resize Is Completed
Is Safari on iOS 6 Caching $.Ajax Results
Why Does Firebase Lose Reference Outside the Once() Function