【百度知道總結】獲取一個圖片地址中圖片名稱的方法

weixin_33831673發表於2013-12-25

描述:

圖片地址為:

"http://snowinmay.net/img/dada.jpg"

圖片名稱:

dada.jpg

======================

方法一:用js的split方法:

var url = "http://snowinmay.net/img/dada.jpg";    
    var reg = /\/.*[^\/]$/g;
    var arr = url.split('/');
    console.log(arr[arr.length-1]);

方法二:用lastIndexOf和substr:

    var index = url.lastIndexOf("/");
    console.log(url.substr(index+1));

方法三:正規表示式:

var url = "http://snowinmay.net/img/dada.jpg";
    var fullpath = "D:\\pic\\02.jpg";
    var reg2 = /^.+\\([^\\]+)$/;
    var reg3 = /^.+\/([^\/]+)$/;
    var filename = fullpath.replace(reg2,"$1");
    console.log(filename);
    console.log(url.replace(reg3,"$1"));

  

相關文章