Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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 JS:如何获取站点名称?_Javascript_Regex_Url - Fatal编程技术网

Javascript JS:如何获取站点名称?

Javascript JS:如何获取站点名称?,javascript,regex,url,Javascript,Regex,Url,示例URL: let str='1〕https://www.google.com.ua/images/branding/product/ico/googleg_lodp.ico“ 让str1='1'https://www.google.com/images/branding/product/ico/googleg_lodp.ico“ 让str2='1'https://books.google.com.ua/images/branding/product/ico/googleg_lodp.ico

示例URL:

  • let str='1〕https://www.google.com.ua/images/branding/product/ico/googleg_lodp.ico“
  • 让str1='1'https://www.google.com/images/branding/product/ico/googleg_lodp.ico“
  • 让str2='1'https://books.google.com.ua/images/branding/product/ico/googleg_lodp.ico“

如何从这些URL中仅获取站点名称?只有单词-google.

以下模式将返回您想要的组名:

[.+:\/\/]?[^.]*\.?(?<name>[^.]+).+
[.+:\/\/]?[^.]*\.?(?[^.]+)+

它将在类似
URL的情况下工作https://google.com.br
,例如。如果您认为这种方式足够,请告诉我

编辑:

很难像我所说的那样治疗:

  • books.google.com
  • books.londrina.br

在第一种情况下,正确的返回应该是
google
,但在第二种情况下,理想的结果是
books

这相当有效:

https?*?(\w{4,})(?=(?:\。\w{2,3})+\/)

该名称在组1中


。最好使用TLD的白名单。

你说的站点名称是什么意思?比如说,对于str2,你期待什么?Sarath Kumar,这个答案不合适。没有考虑到子域和二级域。那么,你对str2有什么期待?samnu pel,是的,网站名Koushik Chatterjee,str-google,str1-google,str2-google如果我理解正确的话,这不会影响子域。你能做这个js函数吗?看。你能做js函数吗?这里是一个例子:几乎是我需要的。如何添加www?和http