Solved

JavaScript: Filename without path or extension

Posted on 2011-03-10
2
851 Views
Last Modified: 2012-06-22
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
Comment
Question by:hankknight
2 Comments
 
LVL 42

Assisted Solution

by:David S.
David S. earned 100 total points
ID: 35102538
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
 
LVL 20

Accepted Solution

by:
Proculopsis earned 400 total points
ID: 35106391

// Here's a slightly more concise alternative:

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now