JavaScript 中字符串匹配的 Switch 语句 - Switch statement for string matching in JavaScript

10/14 16:32
阅读数 0

问题:

How do I write a swtich for the following conditional?如何为以下条件编写开关?

If the url contains "foo", then settings.base_url is "bar".如果 url包含“foo”,则 settings.base_url 是“bar”。

The following is achieving the effect required but I've a feeling this would be more manageable in a switch:以下是实现所需的效果,但我觉得这在 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/"
    };
}

解决方案:

参考一: https://en.stackoom.com/question/C9Xm
参考二: https://stackoom.com/question/C9Xm
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部