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
2 Answers
Reset to default 5Look 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]