12

I am starting with domains that look like this:

www.exemple.com
main.testsite.com
www.ex-emple.com.ar
main.test-site.co.uk
en.tour.mysite.nl
www.ip.com
www.one.lv

and I need to remove the subdomains to get values that look like this:

exemple.com
testsite.com
ex-emple.com.ar
test-site.co.uk
mysite.nl
ip.com
one.lv

other top level domains:

.ac
.ad
.ae
.af
.ag
.ai
.al
.am
.ao
.aq
.ar
.as
.asia
.at
.au
.aw
.ax
.az
.ba
.bb
.bd
.be
.bf
.bg
.bh
.bi
.bj
.bm
.bn
.bo
.br
.bs
.bt
.bw
.by
.bz
.ca
.cc
.cd
.cf
.cg
.ch
.ci
.ck
.cl
.cm
.cn
.co
.co.ao
.co.ba
.co.bw
.co.cc
.co.ck
.co.cr
.co.fk
.co.id
.co.il
.co.im
.co.in
.co.jp
.co.ke
.co.kr
.co.ls
.co.ma
.co.mz
.co.nl
.co.nz
.co.th
.co.tz
.co.ug
.co.uk
.co.uz
.co.ve
.co.vi
.co.za
.co.zm
.co.zw
.com
.com.af
.com.ag
.com.ai
.com.al
.com.ar
.com.au
.com.aw
.com.az
.com.bb
.com.bd
.com.bh
.com.bi
.com.bm
.com.bn
.com.bo
.com.br
.com.bs
.com.bt
.com.by
.com.bz
.com.cm
.com.cn
.com.co
.com.cu
.com.cv
.com.cy
.com.do
.com.dz
.com.ec
.com.ee
.com.eg
.com.es
.com.et
.com.fj
.com.ge
.com.gh
.com.gi
.com.gl
.com.gn
.com.gp
.com.gr
.com.gt
.com.gu
.com.hk
.com.hn
.com.hr
.com.ht
.com.jm
.com.jo
.com.kg
.com.kh
.com.ki
.com.kw
.com.ky
.com.kz
.com.lb
.com.lc
.com.lk
.com.lr
.com.lv
.com.ly
.com.mg
.com.mk
.com.mm
.com.mo
.com.mt
.com.mu
.com.mv
.com.mw
.com.mx
.com.my
.com.na
.com.nf
.com.ng
.com.ni
.com.np
.com.nr
.com.om
.com.pa
.com.pe
.com.pg
.com.ph
.com.pk
.com.pl
.com.pr
.com.ps
.com.pt
.com.py
.com.qa
.com.ro
.com.sa
.com.sb
.com.sc
.com.sg
.com.sl
.com.sn
.com.sv
.com.sy
.com.tj
.com.tn
.com.tr
.com.tt
.com.tw
.com.ua
.com.uy
.com.uz
.com.vc
.com.ve
.com.vi
.com.vn
.com.ye
.cr
.cv
.cx
.cy
.cz
.de
.dj
.dk
.dm
.do
.dz
.ec
.edu
.ee
.eg
.es
.eu
.fi
.fj
.fk
.fm
.fo
.fr
.ga
.gd
.ge
.gf
.gg
.gh
.gi
.gl
.gm
.gn
.gov
.gp
.gq
.gr
.gs
.gt
.gu
.gy
.hk
.hm
.hn
.hr
.ht
.hu
.id
.ie
.il
.im
.in
.io
.iq
.is
.it
.je
.jm
.jo
.jp
.ke
.kg
.kh
.ki
.km
.kn
.kr
.kw
.ky
.kz
.la
.lb
.lc
.li
.lk
.lr
.ls
.lt
.lu
.lv
.ly
.ma
.mc
.md
.me
.mg
.mil
.mk
.ml
.mm
.mn
.mo
.mobi
.mp
.mq
.mr
.ms
.mt
.mu
.mv
.mw
.mx
.my
.na
.name
.nc
.ne
.ne.jp
.net
.net.ck
.net.cm
.net.in
.nf
.ng
.ni
.nl
.no
.np
.nr
.nu
.nz
.og.ao
.om
.or.th
.org
.org.ck
.org.cn
.org.ls
.pa
.pe
.pf
.pg
.ph
.pk
.pl
.pm
.pn
.pr
.pro
.ps
.pt
.py
.qa
.re
.ro
.rs
.ru
.rw
.sa
.sb
.sc
.se
.sg
.sh
.si
.sk
.sl
.sm
.sn
.so
.sr
.st
.su
.sv
.sy
.tc
.td
.tf
.tg
.th
.tj
.tk
.tl
.tm
.tn
.to
.tr
.tt
.tv
.tw
.tz
.ua
.ug
.uk
.us
.uy
.uz
.vc
.ve
.vg
.vi
.vn
.vu
.wf
.ws
.xxx
.ye
.yt
.za
.zm
.zw
Prestaul
  • 80,402
  • 10
  • 82
  • 84
usr13
  • 121
  • 1
  • 4
  • 3
    With the `.replace` method: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace. If you have problems with your code, please post it. – Felix Kling May 02 '12 at 17:18
  • 10
    OK I'm confused. "not a real question" - I'm a SO newbie but really? Not a _good_ question, probably. No code posted, definitely. But it's clearly a question. – Rob I May 02 '12 at 17:29
  • 2
    What's doubly confusing is someone who both answers the question _and_ votes to close it. If it's not a real question, don't answer it. – alan May 02 '12 at 17:37
  • 2
    Good question and bad close by moderators. What was wrong with this question? Someone who voted to close it absolutely has to post an explanation. Irresponsible to vote to close and not provide feedback. – Prestaul May 02 '12 at 17:42
  • In order to do this you'll have to know the valid TLDs and as such I have added them back into the question. It may help inform those posting their answers. – Prestaul May 02 '12 at 17:45
  • @Prestaul: The question sounds like: "I have this, I want this. Do it!". At least this is why I voted to close it. – Felix Kling May 02 '12 at 17:46
  • @FelixKling, you are right! He gave inputs and outputs and asked for help with the guts! What else did you want? – Prestaul May 02 '12 at 17:48
  • @Prestaul: I want people to try something on their own and don't let other people do their work... The OP didn't show any effort to solve this on his/her own. As you can see, I'm at least trying to point him/her into the right direction and actually spend a lot of time trying to find duplicates... – Felix Kling May 02 '12 at 17:53
  • Have a look at these two: [Regex for extracting second level domain from FQDN?](http://stackoverflow.com/questions/1774541/regex-for-extracting-second-level-domain-from-fqdn) and [Need a regular expression to capture second level domain (SLD)](http://stackoverflow.com/questions/4452916/need-a-regular-expression-to-capture-second-level-domain-sld), especially the second one. – Felix Kling May 02 '12 at 18:02
  • 2
    @FelixKling, you would have been happy if he had posted some code that didn't work? I'll grant that there are plenty of duplicates (http://stackoverflow.com/questions/3439863/javascript-regex-for-finding-just-the-root-domain-name-without-sub-domains), but you didn't close as a duplicate. Exactly what did you want from the OP that wasn't there? – Prestaul May 02 '12 at 18:15
  • @Prestaul: Effort. That's just my opinion though. 4 other people might have had other or similar opinions. Two people think the question is fine. That's the community. If you want a "higher order" opinion I suggest you take this to meta. We can also reopen it and close it again as duplicate if this is more appropriate. But in the end this question would not add any value either way. – Felix Kling May 02 '12 at 18:16
  • [psl](https://www.npmjs.com/package/psl) library on npm does what you are asking. Code: `const domain = psl.get(host);`. It uses Mozilla's [Public suffix list](https://publicsuffix.org/list/) which is considered the official resource for this information. – chickens Feb 13 '22 at 01:27

1 Answers1

9
var result = string.replace(/^[^.]+\./g, "");
Alexander Pavlov
  • 30,691
  • 5
  • 65
  • 91
  • Any reason for the downvote? Is it the case that OP wanted to retain exactly the second-level domains, considering things like `co.uk` as a TLD? – Alexander Pavlov May 02 '12 at 17:22
  • 1
    I gave you a +1, but I guess @usr13's "en.tour.mysite.nl" => "mysite.nl" replacement doesn't work with your answer, so maybe they are looking for "domain" ".com" "optional .uk" and not always removing the _first_ piece? – Rob I May 02 '12 at 17:26
  • @RobI: thanks for the hint, it's actually hard to deduce the common rule for the replacement. – Alexander Pavlov May 02 '12 at 17:26
  • regex is a bit like the rubix cube. Will be very interesting to see if a single regex can handle all the domains given without reference to an allowed extensions array – Claude May 02 '12 at 17:38
  • 3
    This doesn't seem like it would work for multiple subdomains such as "en.tour.mysite.nl" – gimp3695 May 09 '19 at 23:47
  • 1
    @gimp3695 This works with multiple subdomains: var result = string.split(".").slice(-2).join(".") It doesn't work with domains such as "co.uk". – Guido Jan 06 '20 at 10:24