JS文件名和路徑截取分割

常用方法有兩種

以/Users/lz/project/test.txt爲例分別介紹一下

方法一、

fullPath = '/Users/lz/project/test.txt';
pos = fullPath.lastIndexOf('/');
fileName = fullPath.substr(pos+1);
console.log(fileName);
filePath = fullPath.substr(0,pos);
console.log(filePath);

方法二、

如果你已經知道文件名,那就更好辦了。

fullPath = '/Users/lz/project/test.txt';
fileName = 'test.txt';
filePath = fullPath.replace(fileName, '');
console.log(filePath);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章