JavaScript中字符串匹配的switch语句

2022-01-19 00:00:00 regex switch-statement javascript

如何为以下条件编写开关?

How do I write a switch for the following conditional?

如果 url 包含foo",则 settings.base_url 是bar".

If the url contains "foo", then settings.base_url is "bar".

以下实现了所需的效果,但我觉得这在 switch 中会更易于管理:

The following is achieving the effect required but I've a feeling this would be more manageable in a switch:

var doc_location = document.location.href;
var url_strip = new RegExp("http://.*/");
var base_url = url_strip.exec(doc_location)
var base_url_string = base_url[0];

//BASE URL CASES

// LOCAL
if (base_url_string.indexOf('xxx.local') > -1) {
    settings = {
        "base_url" : "http://xxx.local/"
    };
}

// DEV
if (base_url_string.indexOf('xxx.dev.yyy.com') > -1) {
    settings = {
        "base_url" : "http://xxx.dev.yyy.com/xxx/"
    };
}

推荐答案

你不能在 switch 中做到这一点,除非你正在做 full 字符串匹配;这是在进行 substring 匹配. (这不是完全正确,正如 Sean 在评论中指出的那样.请参阅末尾的注释.)

You can't do it in a switch unless you're doing full string matching; that's doing substring matching. (This isn't quite true, as Sean points out in the comments. See note at the end.)

如果您很高兴顶部的正则表达式删除了您不想在匹配中比较的所有内容,那么您不需要子字符串匹配,并且可以这样做:

If you're happy that your regex at the top is stripping away everything that you don't want to compare in your match, you don't need a substring match, and could do:

switch (base_url_string) {
    case "xxx.local":
        // Blah
        break;
    case "xxx.dev.yyy.com":
        // Blah
        break;
}

...但同样,这仅在您匹配的 complete 字符串时才有效.如果 base_url_string 是yyy.xxx.local",而您当前的代码将与xxx.local"分支中的匹配,它将失败.

...but again, that only works if that's the complete string you're matching. It would fail if base_url_string were, say, "yyy.xxx.local" whereas your current code would match that in the "xxx.local" branch.

更新:好的,所以从技术上讲,您可以使用 switch 进行子字符串匹配,但在大多数情况下我不推荐它.下面是方法(现场示例):

Update: Okay, so technically you can use a switch for substring matching, but I wouldn't recommend it in most situations. Here's how (live example):

function test(str) {
    switch (true) {
      case /xyz/.test(str):
        display("• Matched 'xyz' test");
        break;
      case /test/.test(str):
        display("• Matched 'test' test");
        break;
      case /ing/.test(str):
        display("• Matched 'ing' test");
        break;
      default:
        display("• Didn't match any test");
        break;
    }
}

这是因为 JavaScript switch 语句的工作方式,特别是两个关键方面:首先,以 source text 顺序考虑 case,其次,选择器表达式(关键字 case 之后的位)是 表达式在这种情况下被评估(而不是像其他一些语言中的常量).因此,由于我们的测试表达式是 true,第一个导致 truecase 表达式将被使用.

That works because of the way JavaScript switch statements work, in particular two key aspects: First, that the cases are considered in source text order, and second that the selector expressions (the bits after the keyword case) are expressions that are evaluated as that case is evaluated (not constants as in some other languages). So since our test expression is true, the first case expression that results in true will be the one that gets used.

相关文章