Switch statement for string matching in JavaScript Switch statement for string matching in JavaScript javascript javascript

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.


RegExp can be used on the input string with the match method too.

Output of the match method is an array so we need to retrieve the first array element.

When match fails it returns null. To avoid an exception error we use optional chaining or the logical || conditional operator before accessing the first array element and test against the input property that is a static property of regular expressions that contains the target input string.

const str = 'XYZ test';switch (str) {  case str.match(/^xyz/)?.input:    console.log("Matched a string that starts with 'xyz'");    break;  case str.match(/test/)?.input:    console.log("Matched the 'test' substring");            break;  default:    console.log("Didn't match");    break;}

Another approach is to use the String() constructor to convert the resulting array that must have only 1 element (no capturing groups) and whole string must be captured with quanitifiers (.*) to a string. In case of a failure the null object will become a 'null' string. That may seem less convenient.

const str = 'haystack';switch (str) {  case String(str.match(/^hay.*/)):    console.log("Matched a string that starts with 'hay'");    break;}

Anyway, a more elegant solution is to use the test method instead of match, i.e. /^find-this-in/.test(str) with switch (true) which simply returns a boolean value and it's easier to match without case sensitivity.

const str = 'haystack';switch (true) {  case /^hay.*/.test(str):    console.log("Matched a string that starts with 'hay'");    break;}


Just use the location.host property

switch (location.host) {    case "xxx.local":        settings = ...        break;    case "xxx.dev.yyy.com":        settings = ...        break;}