UNPKG

863 BJavaScriptView Raw
1const path = require('path');
2
3// function getDirNameFromPath(targetPath) {
4// // 路径为 ./xxx 类型时要先去掉 ./,然后再将所有的 / 替换为 _
5// return targetPath
6// .replace(/^\.([^[\\||\/])*[\\||\/]/gi, '')
7// .replace(new RegExp(path.sep.replace(/\\/gi, '\\\\'), 'gi'), '_')
8// .replace(/\./, '_');
9// }
10//
11// console.log(getDirNameFromPath('./page_rule/cases/basic-check'));
12// console.log(getDirNameFromPath('page_rule/cases/basic-check'));
13// console.log(getDirNameFromPath(__dirname));
14// console.log(getDirNameFromPath(__filename));
15
16function test(str, id) {
17 // return str.replace(/\.(.*)/, `_${id}.${$1}`);
18 return str.replace(/(.*)\.(.*)/, function (match, p1, p2) {
19 return [p1, `${id}.${p2}`].join('_');
20 });
21}
22
23console.log(test('xxx/yy.png', 2));
24console.log(test('xxx/yy.jpg', 5));
\No newline at end of file