首页 > Web开发 > 详细

JS 获取一串路径中的文件名称

时间:2021-05-31 16:06:53      阅读:13      评论:0      收藏:0      [点我收藏+]

1.带后缀名

function getFileName(path){
        var pos1 = path.lastIndexOf(‘/‘);
        var pos2 = path.lastIndexOf(‘\\‘);
        var pos  = Math.max(pos1, pos2)
        if( pos<0 )
            return path;
        else
            return path.substring(pos+1);
    }
原文链接:https://blog.csdn.net/qaakd/article/details/108058545

2.不带后缀名

 getFileName(path) {
      var pos1 = path.lastIndexOf(‘/‘)
      var pos2 = path.lastIndexOf(‘\\‘)
      var pos = Math.max(pos1, pos2)
      if (pos < 0) {
        return path
      }
      else {
        let tempPath = path.substring(pos + 1);
        return tempPath.substring(0, tempPath.lastIndexOf("."));
      }
    }

JS 获取一串路径中的文件名称

原文:https://www.cnblogs.com/maycpou/p/14830850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!