• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1264
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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