简明的方法来比较多个值
问题描述:
请考虑以下事项:
var a = 'jesus';
if(a == 'something' || a == 'nothing' || a=='anything' || a=='everything'){
alert('Who cares?');
}
有没有办法让它缩短?
Javascript中是否有任何内容,如 if(a =='bbb'||'ccc')
?
Is there anything in Javascript like if (a=='bbb'||'ccc')
?
此外,jQuery可以在这里帮忙吗?
In addition, can jQuery help here?
答
使用正则表达式:
if (/^(something|nothing|anything|everything)$/.exec('jesus')) alert('Who cares?');
或相反:
/^(something|nothing|anything|everything)$/.exec('jesus')||alert('Who cares?');
[更新]更短; - )
[Update] Even shorter ;-)
if (/^(some|no|any|every)thing$/.exec('jesus')) alert('Who cares?');