views:

48

answers:

3

I have a full path to an image, which I am using jquery to read, in the form $('img.my_image').attr('src') however I just want the filename portion (discarding the path).

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

+3  A: 
var index = yourstring.lastIndexOf("/");
var filename = yourstring.substr(index);
Gregoire
+1  A: 
function getFileName(path) {
return path.match(/[-_\w]+[.][\w]+$/i)[0];
}
XGreen
+1  A: 

In Javascript you could do

function getFileNameFromPath(path) {
  var ary = path.split("/");
  return ary[ary.length - 1];
}
Robusto