How to extract the host from a URL in JavaScript?

2019-04-18 15:19发布

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

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

5条回答
在下西门庆
2楼-- · 2019-04-18 15:44

If you actually have valid URLs, this will work:

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 A elements.

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

Upon further consideration, I looked into the Node.js docs and found this little gem: 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

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

查看更多
姐就是有狂的资本
3楼-- · 2019-04-18 15:55

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

var URL = require('url')
var url = URL.parse('https://stackoverflow.com/q/13506460/2535178')
console.log(url.hostname)
//=> stackoverflow.com
查看更多
冷血范
4楼-- · 2019-04-18 15:58

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

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);
});
查看更多
劳资没心,怎么记你
5楼-- · 2019-04-18 16:08
/^((?:[a-z0-9-_]+\.)*[a-z0-9-_]+\.?)(?::([0-9]+))?(.*)$/i

matches are host, port, path

查看更多
Bombasti
6楼-- · 2019-04-18 16:08

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

This seems to be a more current way.

查看更多
登录 后发表回答