user804401 user804401 - 3 months ago 18
Javascript Question

Split url based on environment

I have the below urls based on environment separated by hyphen.


  1. www-dev.someurl.com for dev environment

  2. www-qa.someurl.com for qa environment

  3. www.someurl.com for production, no environment prefix



In one my code snippets i want to dynamically build the above url
based on environment by reading from url (either using $location.host() or window.location.hostname)

can anyone suggest a regex or some code to find out if in the Url there is a envirnment variable (dev/qa)

if(contains dev/qa){
//build url
} else {
//build production url
}


Please suggest the best option using regex/code snippet

Answer

As per your earlier comment 'rather than passing qa/dev in regex is there any way where i can get value after hyphen' , how about this instead of regex :

 if((window.location.hostname).indexOf('-')>=0){
     var env= (window.location.hostname).split('-')[1];
     // possible output of env : dev.someurl.com,qa.someurl.com
    }
    else{
    //prod
    }
Comments