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".

Tom
  • 950
  • 3
  • 15
  • 27
  • 1
    You will need a list of valid top-level/second-level domains to do this. – Gumbo Aug 10 '10 at 13:48
  • Is this for the domain address of the currently loaded page, or just for any domain you want to put through the function? – IcyBlueRose Aug 10 '10 at 14:49
  • @Gumbo: So there's no way of doing it with out a list? @IcyBlueRose: Yeah it's not the currently loaded page, its any domain that goes through it. – Tom Aug 10 '10 at 15:35

1 Answers1

0

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

Regex: To pull out a sub-string between two tags in a string

Regex to replace all \n in a String, but no those inside [code] [/code] tag

Need regexp to find substring between two tokens

Community
  • 1
  • 1
The Mirage
  • 378
  • 2
  • 11