Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中使用switch_Javascript_String_Switch Statement - Fatal编程技术网

在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