简体   繁体   English

如果满足条件,则返回 true

[英]returning true if a condition is met

I have to color a div based on the file extension.我必须根据文件扩展名为 div 着色。 However, sometimes the file extension may not match with any of the file extensions that i have mentioned below so in that case I will color the div white.但是,有时文件扩展名可能与我在下面提到的任何文件扩展名都不匹配,因此在这种情况下,我会将 div 着色为白色。 But with my condition below, it's always white even if the file extension matches with 'docx', 'doc', 'txt' what am I doing wrong here?但是在我下面的情况下,即使文件扩展名与'docx', 'doc', 'txt'匹配,它也总是白色的,我在这里做错了什么?

if(.this.fileExtension.toLowerCase(),match('docx', 'doc', 'txt')) return ''

In the example, you are only matching with the 'docx' extension, maybe encapsulating your code in a for loop that checks for all needed examples can be a solution:在示例中,您仅与“docx”扩展名匹配,也许将您的代码封装在一个检查所有需要的示例的 for 循环中可能是一个解决方案:

let found = False;
for (ext in ["docx", "doc", "txt"]) {
     if (this.fileExtension.toLowerCase().match(ext)) {
         found = True;
         break;
     }
}
// work your magic with the found variable...

I take a look at the match javascript documentation: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/String/match我看一下比赛 javascript 文档: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/String/match

I think this code is acceptable for what your are trying to do:我认为这段代码对于您正在尝试做的事情是可以接受的:

const extension = "html"

if(!extension.match(/^(doc|docx|txt)$/i)) {
    console.log("white");
}

// print white

The 'i' parameter is for regex case insensitive 'i' 参数用于正则表达式不区分大小写

You can do that too你也能做到

return extension.match(/^(doc|docx|txt)$/i) ? "chose a color here" : "white";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM