简体   繁体   中英

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

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

 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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM