BRond
BRond

Reputation: 57

How to get the first string from the URL?

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

Upvotes: 0

Views: 483

Answers (3)

Sanjay
Sanjay

Reputation: 515

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.

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

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);

Upvotes: 1

Pak Wah Wong
Pak Wah Wong

Reputation: 506

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

Upvotes: 0

Related Questions