In this switch statement, why does it fail in the regex check and falls to the default case?
The regex matches when I do it as if ... else, but, it doesn't matches when I do it as a switch statement and it falls to the default case!
let siteUrl = 'http://example.com';
let check1 = checkSite(siteUrl);
let check2 = checkSite();
function checkSite(siteUrl) {
let url = (siteUrl) ? siteUrl : 'http://example.com';
let regex = /^http:\/\/example\.com$/gi;
let result = {};
if (regex.test(url)) {
console.log('regex match!');
} else {
console.log('regex does NOT match!');
}
switch (true) {
case regex.test(url) : //<-- Why this fails?
console.log('case 1');
result.case = 1;
break;
case /another-regex/.test(url) :
console.log('case 2');
result.case = 2;
break;
default : //<-- Why do I get here?
console.log('default');
result.case = 3;
}
console.log(result);
return result;
}