我是打字稿的新手,我想從檔案名中洗掉時間戳。檔案名可以是這樣
的FileName_20210210050124592.xml,或者FileName_20210210050124592_2.xml
我只想洗掉時間戳(我想在第二個檔案名中保留 _2)這是我迄今為止嘗試過的方法,
let fileName: string = "FileName_20210210050124592.xml";
let newFileName: string;
if((fileName.match(/_/g)||[]).length>1){
newFileName = fileName.replace(/_.*_/, '_');
}
else{
newFileName = fileName.replace(/_.*\./, '.');
}
console.log(newFileName);
有沒有更好的方法或者我可以改進這段代碼?
uj5u.com熱心網友回復:
您需要決定如何區分時間戳和檔案編號。
下面的示例假設時間戳至少有 12 位數字,而檔案編號則少于該數字:
let fileName = "FileName_20210210050124592.xml";
let newFileName = fileName.replace(/_\d{12}\d*/, '');
console.log(newFileName);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450481.html
標籤:javascript 节点.js 打字稿
