Regexp in Switch Statement

Switch statement for string matching in JavaScript

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;
}

...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.


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;
}
}

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.

Switch Case statement for Regex matching in JavaScript

You need to use a different check, not with String#match, that returns an array or null which is not usable with strict comparison like in a switch statement.

You may use RegExp#test and check with true:

var regex1 = /a/,    regex2 = /b/,    regex3 = /c/,    samplestring = 'b';
switch (true) { case regex1.test(samplestring): console.log("regex1"); break; case regex2.test(samplestring): console.log("regex2"); break; case regex3.test(samplestring): console.log("regex3"); break;}

regexp in switch statement

Switch-case statement works like if-elseif.

As well as you can use regex for if-elseif, you can also use it in switch-case.

if (preg_match('/John.*/', $name)) {
// do stuff for people whose name is John, Johnny, ...
}

can be coded as:

switch $name {
case (preg_match('/John.*/', $name) ? true : false) :
// do stuff for people whose name is John, Johnny, ...
break;
}

Can I match regular expressions in switch statements in R?

We can do this with tidyverse methods

  1. Convert the string to a tibble/data.frame
  2. Remove the characters not neeeded with str_remove_all
  3. Then, separate the column into two by specifying the sep
  4. Get the rowMeans
library(dplyr)
library(tidyr)
library(stringr)
tibble(mystring) %>%
mutate(mystring = str_remove_all(mystring, "[A-Za-z.><]+")) %>%
separate(mystring, into = c('col1', 'col2'), sep="[- ]+",
convert = TRUE) %>%
transmute(out = rowMeans(., na.rm = TRUE))

-output

# A tibble: 3 x 1
out
<dbl>
1 55
2 50
3 50

data

mystring <- c("50-60", "ca. 50", ">50")

Regex as switch case

Use an object holding the regular expressions, instead of a switch statement.

var highlighted = lyrics.replace(/\[.*\]/g, function (match) {

var regex = {
intro: /\[Intro.*?\]/g,
verse: /\[Verse.*?\]/g,
prechorus: /\[Pre-Chorus.*?\]/g,
chorus: /\[Chorus.*?\]/g,
bridge: /\[Bridge.*?\]/g,
outro: /\[Outro.*?\]/g
};

for (var k in regex) {
if (regex.hasOwnProperty(k)) {
if (regex[k].test(match)) {
return span[k] + match + span.close;
}

}
}

});

JSFiddle example

http://jsfiddle.net/unwthvns/1/

Switch statements can usually always be replaced with an object in JavaScript. No need for them.

Switch Case Regex Test

switch-case doesn't work that way.

regex.test() method returns a boolean (true/false) and you are comparing that against the input string itself which will not be true for any of the case statement.

You need to convert switch-case into multiple if / else if / else block to execute your logic.



Related Topics



Leave a reply



Submit