Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:12:03+00:00 2026-05-14T23:12:03+00:00

How do I write a switch for the following conditional? If the url contains

  • 0

How do I write a switch for the following conditional?

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

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/"
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:12:04+00:00Added an answer on May 14, 2026 at 11:12 pm

    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.

    Otherwise, while you can use a switch for substring matching, but I wouldn’t recommend it in most situations (more below). Here’s how it would look:

    function test(str) {
        switch (true) {
            case /xyz/.test(str):
                console.log("• Matched 'xyz' test");
                break;
            case /test/.test(str):
                console.log("• Matched 'test' test");
                break;
            case /ing/.test(str):
                console.log("• Matched 'ing' test");
                break;
            default:
                console.log("• Didn't match any test");
                break;
        }
    }
    
    function test(str) {
        console.log("Testing '" + str + "':");
        switch (true) {
            case /xyz/.test(str):
                console.log("• Matched 'xyz' test");
                break;
            case /test/.test(str):
                console.log("• Matched 'test' test");
                break;
            case /ing/.test(str):
                console.log("• Matched 'ing' test");
                break;
            default:
                console.log("• Didn't match any test");
                break;
        }
    }
      
    test("testing");
    test("xyz123");
    test("foo");
    test("fooing");
    .as-console-wrapper {
        max-height: 100% !important;
    }

    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.

    The reason I wouldn’t recommend it in most situations is that it’s cumbersome as well as being somewhat surprising (to people reading it later) compared to the equivalent if/else if/else:

    function test(str) {
        if (/xyz/.test(str)) {
            console.log("• Matched 'xyz' test");
        } else if (/test/.test(str)) {
            console.log("• Matched 'test' test");
        } else if (/ing/.test(str)) {
            console.log("• Matched 'ing' test");
        } else {
            console.log("• Didn't match any test");
        }
    }
    

    Live Example:

    function test(str) {
        console.log("Testing '" + str + "':");
        if (/xyz/.test(str)) {
            console.log("• Matched 'xyz' test");
        } else if (/test/.test(str)) {
            console.log("• Matched 'test' test");
        } else if (/ing/.test(str)) {
            console.log("• Matched 'ing' test");
        } else {
            console.log("• Didn't match any test");
        }
    }
      
    test("testing");
    test("xyz123");
    test("foo");
    test("fooing");
    .as-console-wrapper {
        max-height: 100% !important;
    }

    In both cases, the code does the same things in the same order, but unless you’re well-versed in JavaScript arcana, the latter is clearer (arguably even if you are).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a class ListNode which has the following properties: int value; ListNode *next; Provide
You write a function and, looking at the resulting assembly, you see it can
I write my app in VS 2008 and so use all the fanciful stuffs
Wanna write a RegEx to validate a driving license. if it doesn't start with
Please write a list of tasks that a copy constructor and assignment operator need
I write a singleton c++ in the follow way: class A { private: static
I write a large static method that takes a generic as a parameter argument.
I write a Text Editor with Java , and I want to add Undo
I write financial applications where I constantly battle the decision to use a double
I write web based applications. Performance is obviously a key factor. Whilst database load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.