Javascript 如何在Fle扩展之前删除文本
我想删除图像文件扩展名前的3个字符,例如图像的此路径 从此Javascript 如何在Fle扩展之前删除文本,javascript,Javascript,我想删除图像文件扩展名前的3个字符,例如图像的此路径 从此 对此 如何在javascript中做到这一点?由于您不够具体,我这样做: // Your array of files const files = [ 'image123.jpg', 'test.jpg', 'file.txt', 'docum123ents.doc' ]; // The part you want to remove from each file you are going to scan const partToRe
对此
如何在javascript中做到这一点?由于您不够具体,我这样做:
// Your array of files
const files = [ 'image123.jpg', 'test.jpg', 'file.txt', 'docum123ents.doc' ];
// The part you want to remove from each file you are going to scan
const partToRemove = '123';
// New array with new files name
let newFiles = [];
console.log(files);
// For each file in files
newFiles = files.map(element => {
// if element includes the part you want to remove
if (element.includes(partToRemove)) {
// replace that part with an empty string and return the element
return element.replace(partToRemove, '');
} else {
// return the element, it's already without 'partToRemove'
return element;
}
});
console.log('------');
console.log(newFiles);
输出:
[ 'image123.jpg', 'test.jpg', 'file.txt', 'docum123ents.doc' ]
------
[ 'image.jpg', 'test.jpg', 'file.txt', 'documents.doc' ]
应该从文件
archive.tar.gz
中删除哪些内容?您能更具体一些吗?是否只删除数字部分?或者您必须指定一个字符串partToRemove='123'
,并且在您扫描的每个文件中,如果存在该部分,您要删除它吗?我已经编辑了我的帖子,很抱歉,OP没有提到在任何地方删除数字部分。@MaheerAli问题不具体。这就是我从这样一个问题中理解的。如果问题将被编辑,而这不是他想要的,我将删除这个答案。@JoKeRxbLaCk抱歉我误解了+1问题需要进一步澄清,您是要删除文件名末尾的任意长度的数字,还是始终为123,或者文件名末尾的数字长度始终为3?提供更多详细信息以获得所需的帮助。