Solved

JavaScript: Filename without path or extension

Posted on 2011-03-10
2
860 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript/JQuery - HtmlEncode (?) Ajax Result 1 67
Html fieldset fix its height and width 4 34
forgetful about js objects 8 48
Filktering Alphabetically 8 32
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

821 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