开发者

Split an image's href tag

开发者 https://www.devze.com 2022-12-19 07:02 出处:网络
I\'d like to split an image\'s href tag like &l开发者_JAVA技巧t;img src=\'/root/myimages/myPic.jpg\'>

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

&l开发者_JAVA技巧t;img src='/root/myimages/myPic.jpg'>

so that I'm getting just myPic.jpg - what's the easiest way?


var src = document.getElementById("yourImgId").src;
src = src.replace(/(.*\/)?([^/]*)$/, "$2");


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

var str = $('#myImg').attr('src');
var newStr = str.substring(str.lastIndexOf("/") + 1);


var t = imageNode.src;
t = (t = t.split('/'))[t.length - 1];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号