2

I have a standard URL e.g.

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

I am using javascript in riak for map reduce and would like to only extract www.test.com. 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

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.org/mozilla-central/source/netwerk/dns/… 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. Commented Dec 9, 2012 at 11:28

2 Answers 2

5

Look at this answer: https://stackoverflow.com/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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.