0

I'm a newbie in web development so pls forgive my newbie question.

I have a URL "https://123asd.my.website.com/blabla/blabla/blabla

What I'm trying to figure out is how do I get the "123asd" so that I can set in on my var. Thank you

BRond
  • 57
  • 8
  • You could try by first getting rid of the https:// string by using the `replace` function. – Nicholas Smith May 16 '18 at 01:47
  • `window.location.hostname` will get you the hostname and split it get the first string – Dean May 16 '18 at 01:47
  • Possible duplicate of [Get domain name without subdomains using JavaScript?](https://stackoverflow.com/questions/9752963/get-domain-name-without-subdomains-using-javascript) – hungrykoala May 16 '18 at 01:55

3 Answers3

1

You can use regex

var url = 'https://123asd.my.website.com/blabla/blabla/blabla';

var number = url.match(/([0-9a-z]{1,})\./)[1];

console.log(number);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

const url = "https://123asd.my.website.com/blabla/blabla/blabla";
let firstStr = url.replace("https://", ""); // get rid of "https://" or you can do it by some other way
firstStr = firstStr.substring(0, firstStr.indexOf('.')); // get the substring start from the beginning to the first '.'

console.log(firstStr); // 123asd
Pak Wah Wong
  • 506
  • 3
  • 8
0
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);

The above code works for both http and https protocol.

Sanjay
  • 515
  • 3
  • 8