Alex Rivera | Logout

Getting just the filename from a path with JavaScript

Asked 2010-03-26T19:19:58.410
33

I have a full path to an image, which I am using jQuery to read like this:

$('img.my_image').attr('src');

However I just want the filename portion (i.e. without full path).

Are there any built-in functions to do this, or would a regex be the only option?

Edit
Report

1 Answer

8
function getFileName(path) {
return path.match(/[-_\w]+[.][\w]+$/i)[0];
}
answered 2010-03-26T19:23:05.113

Your Answer