Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript: Filename without path or extension

Posted on 2011-03-10
2
Medium Priority
?
897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 43

Assisted Solution

by:David S.
David S. earned 400 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 1600 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

618 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