• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

JavaScript: Filename without path or extension

Using JavaScript, how can I get the name of a file without its path or extension?

This:
/home/mydir/Flower.jpg


Should return this:
Flower
0
hankknight
Asked:
hankknight
2 Solutions
 
David S.Commented:
Here's one way to do it:
var path = '/home/mydir/Flower.jpg';
var pathParts = path.split('/'), file = pathParts[pathParts.length-1];
alert(file.split('.')[0]);

Open in new window

0
 
ProculopsisCommented:

// Here's a slightly more concise alternative:

var fileSpec = "/home/mydir/Flower.jpg";
var fileName = fileSpec.replace( /^.*\/(\w+)\..*$/, "$1" );
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now