0

An example of what im trying to get:

String1 - 'string.co.uk' - would return 'string' and 'co.uk'
String2 - 'random.words.string.co.uk' - would return 'string` and 'co.uk'

I currently have this:

var split= [];
var tld_part = domain_name.split(".");
var sld_parts = domain_name.split(".")[0];
tld_part = tld_part.slice(1, tld_part.length);
split.push(sld_parts);
split.push(tld_part.join("."));

With my current code, it takes the split parameter from the beginning, i want to reverse it if possible. With my current code it does this:

String1 - 'string.co.uk' - returns 'string' and 'co.uk'
String2 - 'random.words.string.co.uk' - would return 'random` and 'words.string.co.uk'

Any suggestions?

1
  • You could just slice negative like string.split('.').slice(-3), then build the string. Commented Jun 10, 2013 at 8:03

4 Answers 4

2

To expand upon elclanrs comment:

function getParts(str) {
    var temp = str.split('.').slice(-3) // grabs the last 3 elements
    return {
        tld_parts : [temp[1],temp[2]].join("."),
        sld_parts : temp[0]
    }
}

getParts("foo.bar.baz.co.uk") would return { tld_parts : "co.uk", sld_parts : "baz" }

and

getParts("i.got.99.terms.but.a.bit.aint.one.co.uk") would return { tld_parts : "co.uk", sld_parts : "one" }

Sign up to request clarification or add additional context in comments.

1 Comment

What if theres more random words in the string, like lets say more.random.words.in.string.co.uk, then the -3 wouldnt work right?
0

try this

var str='string.co.uk'//or 'random.words.string.co.uk'
var part = str.split('.');
var result = part[part.length - 1].toString() + '.' + part[part.length - 1].toString();
alert(result);

Comments

0

One way that comes to mind is the following

var tld_part = domain_name.split(".");
var name = tld_part[tld_part.length - 2];
var tld = tld_part[tld_part.length - 1] +"."+ tld_part[tld_part.length];

Comments

0

Depending on your use case, peforming direct splits might not be a good idea — for example, how would the above code handle .com or even just localhost? In this respect I would go down the RegExp route:

function stripSubdomains( str ){
  var regs; return (regs = /([^.]+)(\.co)?(\.[^.]+)$/i.exec( str ))
    ? regs[1] + (regs[2]||'') + regs[3]
    : str
  ;
};

Before the Regular Expression Police attack reprimand me for not being specific enough, a disclaimer:

The above can be tightened as a check against domain names by rather than checking for ^., to check for the specific characters allowed in a domain at that point. However, my own personal perspective on matters like these is to be more open at the point of capture, and be tougher from a filtering point at a later date... This allows you to keep an eye on what people might be trying, because you can never be 100% certain your validation isn't blocking valid requests — unless you have an army of user testers at your disposal. At the end of the day, it all depends on where this code is being used, so the above is an illustrated example only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.