-1

I need some help on constructing a regex, please.

I have an array of domain names and an array of TLDs (quite long). example:

tlds = {
    '.com':{
        1 : '£4.00',
        2 : '£7.50',
        3 : '£11.00',
        4 : '£14.50',
        5 : '£18.00',
    },
    '.org':{
        1 : '£5.00',
        2 : '£10.00',
        3 : '£15.00',
        4 : '£20.00',
        5 : '£25.00',
    },
    '.net':{
        1 : '£6.00',
        2 : '£12.00',
        3 : '£18.00',
        4 : '£24.00',
        5 : '£30.00',
    },
    '.co.uk':{
        1 : '£7.00',
        2 : '£14.00',
        3 : '£21.00',
        4 : '£28.00',
        5 : '£35.00',
    },
    '.com.br':{
        1 : '£9.00',
        2 : '£16.00',
        3 : '£22.00',
        4 : '£28.00',
        5 : '£34.00',
    }
}

domainResults = {
    0 : {
        'domain' : 'anything.com',
        'status' : 'available'
    },
    1 : {
        'domain' : 'anything-weird.org',
        'status' : 'available'
    },
    2 : {
        'domain' : 'anything-amazing.co.uk',
        'status' : 'available'
    }
}

For each domain name I want to assign the correct tld. Each of the domain names in the array always matches one of the TLDs.

I think the solution should be looping each TLD as a REGEX until it matches the domain name I'm looping through.

I know this will be tricky, because I have always at least 26 TLDs and I want to make sure none of them matches the other, to prevent wrong display of pricing (example: a domain name ending in '.com.br' should never be a result of the regex '.com' but only for the tld '.com.br' ).

Cœur
  • 37,241
  • 25
  • 195
  • 267
w3jimmy
  • 692
  • 10
  • 21

1 Answers1

0

assuming there is no sub-domain part in domain string

for(var index in domainResults){
    var domain = domainResults[index].domain;
    var tld = domain.substring(domain.indexOf("."));
    // now tlds[tld] gives the corresponding tld object
    console.log(tlds[tld]);
}
Diode
  • 24,570
  • 8
  • 40
  • 51