Switch statement for string matching in JavaScript(JavaScript中字符串匹配的switch语句)
问题描述
如何为以下条件编写开关?
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/"
};
}
推荐答案
你不能在 (这不是完全正确,正如 Sean 在评论中指出的那样.请参阅末尾的注释.)switch
中做到这一点,除非你正在做 full 字符串匹配;这是在进行 substring 匹配.
You can't do it in a (This isn't quite true, as Sean points out in the comments. See note at the end.)switch
unless you're doing full string matching; that's doing substring matching.
如果您很高兴顶部的正则表达式删除了您不想在匹配中比较的所有内容,那么您不需要子字符串匹配,并且可以这样做:
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
,第一个导致 true
的 case
表达式将被使用.
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.
这篇关于JavaScript中字符串匹配的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript中字符串匹配的switch语句


- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01