How to get basename of a file name?

W

Wild Pete

Is there an equivalent to a "basename" in javascript
that returns the last part of a file name?
For example, given "C:\My Documents\test.txt" and
"/usr/local/test.txt", both should return "test.txt".
Any help would be appreciated.
 
I

Ivo

Is there an equivalent to a "basename" in javascript
that returns the last part of a file name?
For example, given "C:\My Documents\test.txt" and
"/usr/local/test.txt", both should return "test.txt".

Backslashes in strings should be escaped with another backslash, so
use "C:\\My Documents\\test.txt" instead. Then run this regex:

var a="/usr/local/test.txt";
var b=a.match(/[\/|\\]([^\\\/]+)$/);
alert( b[1] );

--Iv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top