-1

I am trying to extract just the websites name from a URL and all the stackoverflow answers haven't led me anywhere.

My URL is in the following format:

https://order-dev.companyname.com

I just want to extract order-dev from the URL.

Orbita1frame
  • 203
  • 4
  • 13

3 Answers3

0

Got this to work for me (?<=\/\/)(.*?)(?=\.)

Orbita1frame
  • 203
  • 4
  • 13
0

If you have an express framework with your node app then it is easy to get a domain/list of subdomains directly from the incoming request..

Using Express Framework

req.hostname() // returns 'example.com'
req.subdomains() // return ['ferrets', 'tobi'] from Host: "tobi.ferrets.example.com"

For plain NodeJS

All Incoming request will contain the following header origin which can be extracted using the following request.headers.origin in any functions where you handle your incoming request MDN LINK

Nishant S Vispute
  • 713
  • 1
  • 7
  • 21
0

In most JavaScript engines (i.e., most browsers (excluding IE) and Node) you can also use URL for this, specifically URL.host combined with String.prototype.split():

const siteUrl = 'https://order-dev.companyname.com';
const myURLObject = new URL(siteUrl);
const subdomain = myURLObject.host.split(".")[0];
console.log(subdomain); // "order-dev"
esqew
  • 42,425
  • 27
  • 92
  • 132
  • O wow! Did not know this. Apparently I have to retrieve a environment variable and append a string to it from within a config file. I'll def keep this solution in my tool bag. Thanks – Orbita1frame May 19 '21 at 01:50