如何在JavaScript中嵌套OR语句? [英] How to nest OR statements in JavaScript?
本文介绍了如何在JavaScript中嵌套OR语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我正在创建一个文件管理器。
Currently I am creating a filemanager.
我想要检查用户是否选择了视频文件。该文件可以是 mov
, f4v
, flv
, mp4
和 swf
。
What I want is to check if the user has selected a video file. The file can be mov
, f4v
, flv
, mp4
and swf
.
我想检查一下var ext
就是其中之一。
I want to check if my var ext
is one of these.
我所拥有的是:
if(ext == ('mov' || 'f4v' || 'flv' || 'mp4' || 'swf'))
{
//Do something
}
有谁知道我怎么能得到这个工作。我不想使用开关,因为我会得到很多案例。
Does anyone know how I can get this working. I don't want to use a switch because I will get a lot of cases.
推荐答案
你需要明确地比较每个值的变量。
You would need to explicitly compare the variable against each of those values.
if( ext === 'mov' || ext === 'f4v' || ... ) {
}
..但是, RegExp 为了救援,我们可以像
..but, RegExp to the rescue, we can go like
if( /mov|f4v|flv|mp4|swf/.test( ext ) ) {
}
这篇关于如何在JavaScript中嵌套OR语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文