views:

35

answers:

4

hi all,

i'd like to split an image's href tag like

<img src='/root/myimages/myPic.jpg'>

so that i'm getting just myPic.jpg - whats the easiest way?

thx

+2  A: 
var src = document.getElementById("yourImgId").src;
src = src.replace(/(.*\/)?([^/]*)$/, "$2");
Pointy
src = src.match(/(.*\/)?([^/]*)$/)[2]
Murali VP
+2  A: 
<img id="myImg" src='/root/myimages/myPic.jpg'>

var str = $('#myImg').attr('src');
var newStr = str.substring(str.lastIndexOf("/") + 1);
gmcalab
+1  A: 
var t = imageNode.src;
t = (t = t.split('/'))[t.length - 1];
Murali VP
Simpler: `t= t.split('/').pop();`
bobince
Yes, thanks, didn't think of pop().
Murali VP
A: 
function getFileName(path) {
 return path.match(/[-_\w]+[.][\w]+$/i)[0];
}
getFileName('http://www.example.com/images/blah.png'); // blah.png

This getFileName() function is awesome because the first part of the regex looks like a guy with an eye patch.

Mathias Bynens