方法1123456789var str = "abcxyz";switch(true) { case /xyz/.test(str) : console.log("1"); break; default: console.log("0"); break;}
方法2123456789var str = "abcxyz";switch(str) { case (str.match(/xyz/) || {}).input : console.log("1"); break; default: console.log("0"); break;}
方法3123456789var str = "abcxyz";switch(str) { case (/xyz/.test(str) ? str : NaN) : //输入值为NaN时不命中 console.log("1"); break; default: console.log("0"); break;}
switch-statement-for-string-matching-in-javascript - Stack Overflow