【百度知道总结】获取一个图片地址中图片名称的方法

描述:

图片地址为:

"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"));

  

posted @ 2013-12-25 18:57  snowinmay  阅读(530)  评论(1编辑  收藏  举报