最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

riak - Using javascript to get sub domain from url - Stack Overflow

programmeradmin1浏览0评论

I have a standard URL e.g.

.html

I am using javascript in riak for map reduce and would like to only extract www.test. So...the domain and the subdomain.

What is the most efficient method to do this in js since I will have millions of records?

Thanks

I have a standard URL e.g.

http://www.test./test1/test2.html

I am using javascript in riak for map reduce and would like to only extract www.test.. So...the domain and the subdomain.

What is the most efficient method to do this in js since I will have millions of records?

Thanks

Share Improve this question asked Dec 9, 2012 at 10:19 TampaTampa 78.5k123 gold badges289 silver badges430 bronze badges 2
  • 1 Tricky, you almost need a database of known domain name extensions because, what happens if you have a url like... example.co.uk? ... A list like this: mxr.mozilla/mozilla-central/source/netwerk/dns/… – Tim Joyce Commented Dec 9, 2012 at 10:25
  • Well...in python I did this. remove http:// and split by /. This the domain was the first element. Just need something in JS. – Tampa Commented Dec 9, 2012 at 11:28
Add a ment  | 

2 Answers 2

Reset to default 5

Look at this answer: https://stackoverflow./a/8498629/623400

var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
var domain = matches && matches[1];  // domain will be null if no match is found

Sophisticated domain matching is kinda tricky, but all this is covered quite well in the linked post.

Try this:

var url = "http://www.test./test1/test2.html";
var domain = url.match(/:\/\/(.[^/]+)/)[1]
发布评论

评论列表(0)

  1. 暂无评论