【发布时间】:2017-11-14 08:41:35
【问题描述】:
所以我有一堆正则表达式,我尝试使用这个 If 语句查看它们是否与另一个字符串匹配:
if(samplestring.match(regex1)){
console.log("regex1");
} else if(samplestring.match(regex2)){
console.log("regex2");
} else if(samplestring.match(regex3)){
console.log("regex3");
}
但是一旦我需要使用更多的正则表达式,这会变得非常难看,所以我想使用这样的 switch case 语句:
switch(samplestring){
case samplestring.match(regex1): console.log("regex1");
case samplestring.match(regex2): console.log("regex2");
case samplestring.match(regex3): console.log("regex3");
}
问题是它不像我在上面的例子中那样工作。 关于它如何工作的任何想法?
【问题讨论】:
-
我想你跳过了
break。 -
匹配后的每个case都会被执行,除非浏览器读取
break关键字。 -
我添加了 break 关键字,但它仍然不起作用
标签: javascript