Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从URL获取第一个字符串?_Javascript_Html_Url - Fatal编程技术网

Javascript 如何从URL获取第一个字符串?

Javascript 如何从URL获取第一个字符串?,javascript,html,url,Javascript,Html,Url,我是网络开发的新手,所以请原谅我的新手问题 我有一个网址 我想弄清楚的是,如何获得123asd,以便我可以设置我的var。谢谢 常量url=https://123asd.my.website.com/blabla/blabla/blabla; 让firstStr=url.replacehttps://,;//摆脱https://或者你可以通过其他方式来实现 firstStr=firstStr.substring0,firstStr.indexOf'.';//获取从开头到第一个“.”的子字符串 c

我是网络开发的新手,所以请原谅我的新手问题

我有一个网址

我想弄清楚的是,如何获得123asd,以便我可以设置我的var。谢谢 常量url=https://123asd.my.website.com/blabla/blabla/blabla; 让firstStr=url.replacehttps://,;//摆脱https://或者你可以通过其他方式来实现 firstStr=firstStr.substring0,firstStr.indexOf'.';//获取从开头到第一个“.”的子字符串 console.logfirstStr;//123asd您可以使用正则表达式

var url='1〕https://123asd.my.website.com/blabla/blabla/blabla'; var number=url.match/[0-9a-z]{1,}\./[1]; console.lognumber;
上面的代码适用于http和https协议。

您可以通过使用替换函数首先删除https://字符串来尝试。window.location.hostname将为您获取主机名并将其拆分以获取第一个字符串可能的重复项
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);