views:

43

answers:

1

In javascript is it possible to create a function that checks an url to find the domain name. EG: if these sites:

www.site.co.uk

www.subdomain.site.co.uk

site.co.uk

subdomain.site.co.uk

were put into the function they would be returned as "site.co.uk".

A: 

I'd say there is a regex solution to this, but it will be complicated. You will have to find the last occurrence of the '.' and get what's after that, then you should get what's between the last and second to last '.', then get anything before the second to last period, up to the third to the right (subdomain) '.'.

Does that make sense?? I'm not sure of the regex required, but here are some questions on SO to get you started...

http://stackoverflow.com/questions/1237/regex-to-pull-out-a-section-a-substing-from-a-string-between-two-tags

http://stackoverflow.com/questions/328387/regex-to-replace-all-n-in-a-string-but-no-those-inside-code-code-tag

http://stackoverflow.com/questions/489567/need-regexp-to-find-substring-between-two-tokens

The Mirage