简体   繁体   中英

Split an image's href tag

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 - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM