Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get domain substring using javascript

I am trying to parse a domain that has more than two period’s in the string.

So for example, I am trying to get sjmktmail-batch1a.marketo.org into marketo.org

I tried using split and a range from the list like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

testdata = sjmktmail-batch1a.marketo.org
testdata.split(".")[1:2]

but that didn’t work.
doing testdata.split(".")[1] brings up marketo but I want marketo.org

Sorry, my main language is python, so some javascript concepts confuse me. I assume you can’t get a range of an array/list by using [x:x]

My main goal is to get a domain like domain.com

so for example:

fdasdadio.conglomo.com would be conglomo.com
billy.fdaoco.codsaso.mainbug.com would be mainbug.com
purple.red.bri.noschool.edu would be noschool.edu

Pretty sure I am missing a concept that would make this easy.

>Solution :

You can do it by a chain of split, slice, join. like this:

slice(-2) means get two last items from array.

const str = 'billy.fdaoco.codsaso.mainbug.com';
const domain = str.split('.').slice(-2).join('.');
console.log(domain)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading