// 下载os组件库 代码可以直接使用
npm install os --save
// 引入os依赖
const os = require('os')
// 函数 lastIndexOf 获取到最后斜杠出现的位置
let filePath = 'D:\\temp\\test.txt';
let index = 0;
if(os.platform().toString() === 'win32'){
index = filePath.lastIndexOf('\\');
}else if(os.platform().toString() === 'linux'){
index = filePath.lastIndexOf('/');
}
// 进行截取 substr
let fileName = filePath.substr(index+1);
// 打印到控制台
console.log('文件名称是',fileName);
微笑的java
欢迎关注转发评论点赞沟通,让编码不在孤单。