16

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
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
usr13
  • 161
  • 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
  • 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
  • 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
    [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
  • @chickens Correct. And one can include it directly to a page like: ` – Marinos An Dec 05 '22 at 15:54
  • @MarinosAn, please don't make edits that substantially change the gist of the question. You are assuming a lot here. – ChrisGPT was on strike Dec 05 '22 at 23:32

2 Answers2

10
var result = string.replace(/^[^.]+\./g, "");
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
  • 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
  • 4
    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
0

psl library on npm does what you are asking.

const domain = psl.get(host);

It uses Mozilla's Public suffix list which is considered the official resource for this information.

chickens
  • 19,976
  • 6
  • 58
  • 55