RegExp 的 Switch 语句变为默认值?
Switch statement for RegExp falling to default?
在这个switch语句中,为什么在regex检查中失败并落入default case?
当我作为 if ... else
执行时,正则表达式匹配,但是,当我作为 switch 语句执行时,它不匹配并且属于默认情况!
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;
}
请注意,当您删除 if
时,请检查代码是否按预期工作:
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 = {};
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;
}
这是因为当您将 RegExp 存储到变量时,它会存储状态。
let regex = /^http:\/\/example\.com$/gi;
const url = "http://example.com";
for (let i = 0; i < 10; i++) {
console.log(regex.test(url));
}
根据 MDN:
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).
When a regex has the global flag set, test() will advance the lastIndex of the regex. (RegExp.prototype.exec() also advances the lastIndex property.)
Further calls to test(str) will resume searching str starting from lastIndex. The lastIndex property will continue to increase each time test() returns true.
Note: As long as test() returns true, lastIndex will not reset—even when testing a different string!
When test() returns false, the calling regex's lastIndex property will reset to 0.
这样试试。
switch (true) {
case url.match(regex) :
console.log('case 1');
result.case = 1;
break;
case url.match(another-regex) :
console.log('case 2');
result.case = 2;
break;
default :
console.log('default');
result.case = 3;
}
希望,成功了!! XD
您也可以改用 match
并检查长度是否大于 0。即使您只是连续记录值,test
似乎也会发生变化。
function checkSite(siteUrl) {
let url = (siteUrl) ? siteUrl : 'http://example.com';
let regex = /^http:\/\/example\.com$/gi;
let result = {};
if (url.match(regex)) {
console.log('regex match!');
} else {
console.log('regex does NOT match!');
}
switch (true) {
case url.match(regex).length > 0 : //<-- this will match
console.log('case 1');
result.case = 1;
break;
case url.match(/another-regex/).length > 0 :
console.log('case 2');
result.case = 2;
break;
default :
console.log('default');
result.case = 3;
}
console.log(result);
return result;
}
let siteUrl = 'http://example.com';
let check1 = checkSite(siteUrl);
let check2 = checkSite();
在这个switch语句中,为什么在regex检查中失败并落入default case?
当我作为 if ... else
执行时,正则表达式匹配,但是,当我作为 switch 语句执行时,它不匹配并且属于默认情况!
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;
}
请注意,当您删除 if
时,请检查代码是否按预期工作:
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 = {};
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;
}
这是因为当您将 RegExp 存储到变量时,它会存储状态。
let regex = /^http:\/\/example\.com$/gi;
const url = "http://example.com";
for (let i = 0; i < 10; i++) {
console.log(regex.test(url));
}
根据 MDN:
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).
When a regex has the global flag set, test() will advance the lastIndex of the regex. (RegExp.prototype.exec() also advances the lastIndex property.)
Further calls to test(str) will resume searching str starting from lastIndex. The lastIndex property will continue to increase each time test() returns true.
Note: As long as test() returns true, lastIndex will not reset—even when testing a different string!
When test() returns false, the calling regex's lastIndex property will reset to 0.
这样试试。
switch (true) {
case url.match(regex) :
console.log('case 1');
result.case = 1;
break;
case url.match(another-regex) :
console.log('case 2');
result.case = 2;
break;
default :
console.log('default');
result.case = 3;
}
希望,成功了!! XD
您也可以改用 match
并检查长度是否大于 0。即使您只是连续记录值,test
似乎也会发生变化。
function checkSite(siteUrl) {
let url = (siteUrl) ? siteUrl : 'http://example.com';
let regex = /^http:\/\/example\.com$/gi;
let result = {};
if (url.match(regex)) {
console.log('regex match!');
} else {
console.log('regex does NOT match!');
}
switch (true) {
case url.match(regex).length > 0 : //<-- this will match
console.log('case 1');
result.case = 1;
break;
case url.match(/another-regex/).length > 0 :
console.log('case 2');
result.case = 2;
break;
default :
console.log('default');
result.case = 3;
}
console.log(result);
return result;
}
let siteUrl = 'http://example.com';
let check1 = checkSite(siteUrl);
let check2 = checkSite();