在javascript中使用switch
开关找不到我的钥匙。我尝试过转换为字符串,也尝试过不转换为字符串,但仍然没有成功。。。。我做错了什么在javascript中使用switch,javascript,string,switch-statement,Javascript,String,Switch Statement,开关找不到我的钥匙。我尝试过转换为字符串,也尝试过不转换为字符串,但仍然没有成功。。。。我做错了什么 var fromTWIL = "i want my key"; console.log(String(fromTWIL)); console.log(String(fromTWIL.match(/key/)[0])); function extractKEY(ft){ var rx = /key/g; var arr = rx.exec(extractKEY); re
var fromTWIL = "i want my key";
console.log(String(fromTWIL));
console.log(String(fromTWIL.match(/key/)[0]));
function extractKEY(ft){
var rx = /key/g;
var arr = rx.exec(extractKEY);
return arr[0];
};
console.log(extractKEY(fromTWIL));
var key = extractKEY(fromTWIL);
console.log("@"+key+"@");
switch (String(fromTWIL)){
//case fromTWIL.match(/key/g)[1]:
case String(fromTWIL.match(/key/g)[0]):
//case extractKEY(fromTWIL):
//case key:
console.log('matched key');
break;
default:
console.log('matched default');
break;
};
FromTwi的
值就是完整的字符串,而.match()
调用的值就是“key”
,所以它们不匹配。你想完成什么?你应该使用if
语句,而不是开关来完成。你应该切换到if