如何从 JavaScript 中的 URL 中提取主机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13506460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:13:54  来源:igfitidea点击:

How to extract the host from a URL in JavaScript?

javascriptnode.jsparsingurl

提问by ThomasReggi

Capture the domain till the ending characters $, \?, /, :. I need a regex that captures domian.comin all of these.

捕获域直到结束字符$, \?, /, :。我需要一个捕获domian.com所有这些的正则表达式。

domain.com:3000
domain.com?pass=gas
domain.com/
domain.com

回答by ma?ek

If you actually have valid URLs, this will work:

如果您确实有有效的 URL,这将起作用:

var urls = [
    'http://domain.com:3000',
    'http://domain.com?pass=gas',
    'http://domain.com/',
    'http://domain.com'
];

for (x in urls) {
    var a = document.createElement('a');
    a.href = urls[x];
    console.log(a.hostname);
}

//=> domain.com
//=> domain.com
//=> domain.com
//=> domain.com

Note, using regex for this kind of thing is silly when the language you're using has other built-in methods.

请注意,当您使用的语言具有其他内置方法时,将正则表达式用于此类事情是愚蠢的。

Other properties available on Aelements.

A元素上可用的其他属性。

var a = document.createElement('a');
a.href = "http://domain.com:3000/path/to/something?query=string#fragment"

a.protocol   //=> http:
a.hostname   //=> domain.com
a.port       //=> 3000
a.pathname   //=> /path/to/something
a.search     //=> ?query=string
a.hash       //=> #fragment
a.host       //=> domain.com:3000


EDIT #2

编辑#2

Upon further consideration, I looked into the Node.js docs and found this little gem: url#parse

经过进一步考虑,我查看了 Node.js 文档并找到了这个小宝石: url#parse

The code above can be rewritten as:

上面的代码可以改写为:

var url = require('url');

var urls = [
    'http://domain.com:3000',
    'http://domain.com?pass=gas',
    'http://domain.com/',
    'http://domain.com'
];

for (x in urls) {
    console.log(url.parse(urls[x]).hostname);
}

//=> domain.com
//=> domain.com
//=> domain.com
//=> domain.com


EDIT #1

编辑#1

See the revision history of this post if you'd like to see how to solve this problem using jsdomand nodejs

如果您想了解如何使用jsdom和解决此问题,请查看此帖子的修订历史记录nodejs

回答by ebohlman

Since you're using node, just use the built-in url.parse()method; you want the resulting hostnameproperty:

既然你用的是node,就用内置的url.parse()方法吧;你想要结果hostname属性:

var url=require('url');
var urls = [
  'http://domain.com:3000',
  'http://domain.com?pass=gas',
  'http://domain.com/',
  'http://domain.com'
];

UPDATED:

更新:

urls.forEach(function(x) {
  console.log(url.parse(x).hostname);
});

回答by Paul Weber

A new challenger has appeared. According to node docs, you can also use

新的挑战者出现了。根据节点文档,您还可以使用

   var url = new URL(urlString);
   console.log(url.hostname);

https://nodejs.org/api/url.html#url_the_whatwg_url_api

https://nodejs.org/api/url.html#url_the_whatwg_url_api

This seems to be a more current way.

这似乎是一种更现代的方式。

回答by rb-

I'm using Node ^10and this is how I extract the hostname from a URL.

我正在使用 Node ^10,这就是我从 URL 中提取主机名的方式。

var url = URL.parse('https://stackoverflow.com/q/13506460/2535178')
console.log(url.hostname)
//=> stackoverflow.com

回答by stroncium

/^((?:[a-z0-9-_]+\.)*[a-z0-9-_]+\.?)(?::([0-9]+))?(.*)$/i

matches are host, port, path

匹配项是主机、端口、路径