user804401 user804401 - 1 year ago 72
Javascript Question

Split url based on environment

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

  1. for dev environment

  2. for qa environment

  3. 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 $ 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 Source

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 :

     var env= (window.location.hostname).split('-')[1];
     // possible output of env :,