gpt4 book ai didi

javascript - 如何以 Angular 获得图像的扩展?

转载 作者:行者123 更新时间:2023-11-30 07:50:31 26 4
gpt4 key购买 nike

这里我使用下面的代码检查图像扩展。 imageName.split(".").pop() 使用 split 和 pop 我得到了图像扩展名,但在此之后我必须检查此扩展名是否与此匹配我必须检查多个或条件,所以代码的制作过程很慢,所以是否有可能减少代码并减少 OR 条件。

例。如果 jpEg extension image 那一次只有一个条件 (extn == jpeg) 是 check all type small,capital extension('jpeg','Jpeg','jPeg','jpEg','jpeG','JPeg','JpEg','JpeG','jPEg','jPeG','jpEG','jPEG ','JPeG','JPEg','JpEG','JPEG') 和 make less or 条件

previewFiles(imageName){
console.log(imagename); // 125.jpEg

var extn = imageName.split(".").pop();

console.log(extn); // jpEg

if((extn == 'pdf') || (extn == 'PDF') || (extn == 'pdF') || (extn == 'pDf') ||
(extn == 'pDF') || (extn == 'PDf') || (extn == 'PdF') || (extn == 'Pdf') ||
(extn == 'jpg') || (extn == 'JPG') || (extn == 'jpG') || (extn == 'jPg') ||
(extn == 'jPG') || (extn == 'JPg') || (extn == 'JpG') || (extn == 'Jpg') ||
(extn == 'gif') || (extn == 'GIF') || (extn == 'giF') || (extn == 'gIf') ||
(extn == 'gIF') || (extn == 'GIf') || (extn == 'GiF') || (extn == 'Gif') ||
(extn == 'png') || (extn == 'PNG') || (extn == 'pnG') || (extn == 'pNg') ||
(extn == 'pNG') || (extn == 'PNg') || (extn == 'PnG') || (extn == 'Png') ||
(extn == 'tif') || (extn == 'TIF') || (extn == 'tiF') || (extn == 'tIf') ||
(extn == 'tIF') || (extn == 'TIf') || (extn == 'TiF') || (extn == 'Tif') ||
(extn == 'jpeg') || (extn == 'Jpeg') || (extn == 'jPeg') || (extn == 'jpEg') ||
(extn == 'jpeG') || (extn == 'JPeg') || (extn == 'JpEg') || (extn == 'JpeG') ||
(extn == 'jPEg') || (extn == 'jPeG' ) || (extn == 'jpEG' ) || (extn == 'jPEG' ) ||
(extn == 'JPeG' ) || (extn == 'JPEg') || (extn == 'JpEG') || (extn == 'JPEG') ||
(extn == 'tiff') || (extn == 'Tiff' ) || (extn == 'tIff') || (extn == 'tiFf' ) ||
(extn == 'tifF') || (extn == 'TIff') || (extn == 'TiFf') || (extn == 'TifF') ||
(extn == 'tIFf') || (extn == 'tIfF') || (extn == 'tiFF') || (extn == 'tIFF') ||
(extn == 'TIfF') || (extn == 'TIFf') || (extn == 'TiFF') || (extn == 'TIFF')){
console.log("extension of image is matched");
}else{
console.log("extension of image is not matched");
}
}

最佳答案

您可以使用使用交替的正则表达式和 /i 进行不区分大小写的比较或使用 toLowerCase 进行字符串比较:

let extn = "TIFf";
const regex = /(?:pdf|jpeg|jpg|tiff?|gif|png)/i;
if (regex.test(extn)) {
console.log(extn);
}

let valid = [
"pdf", "tif", "tiff", "jpg", "jpeg", "gif", "png"
];

if (valid.includes(extn.toLowerCase())) {
console.log(extn);
}

关于javascript - 如何以 Angular 获得图像的扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53897085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com