How to remove the tld with regexp

I have this

// Raw
window.onload = function() {
  
  var str = "https://www.mailchimp.com";
// if str is C:\windows\file system\path\picture name.jpg
alert( str.split('w.').pop().replace(".com","") );
  display("Input: " + str);
  display("Found: " + m);
  
  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
};

Is only replace text end if it is .com. How can select from all char replace? Not depands on what value?

Thanks!

I’m not sure what are you trying to achieve here, really. Do you want to extract domain name from URL without TLD? So

f('https://www.mailchimp.com') // => maichimp
f('https://google.com/about') // => google

If yes, I’m afraid you’re largely underestimating complexity of this task, like a lot!
Consider following valid URLs:

http://example.com/path/to/page?name=ferret&color=purple
https://192.134.34.0:3000/
http://news.com.uk
https://bbc.uk
...

If you still decided to do it, you need to do following steps:

  1. Reliably extract hostname, like so:
function extractHostName(url) {
  return new URL(url).hostname;
}
  1. Have JSON of all possible TLD, so you can detect and eliminate TLD, like so:
function extractDomain(hostname) {
  const tld = JSON.parse(tld.json); // should be in a form of array
  const re = new RegExp(`(${tld.join('|').replace('.', '\\.')})$`);
  return hostname
    .replace(re, '')
    .replace(/\.$/, '')
    .replace(/^(\w+\.)*/, '');
}

You can grab JSON of all TLD here: https://datahub.io/core/top-level-domain-names
NOTE: they are not in array format, so you’ll have to re-format

But honestly, I would just drop this idea :slight_smile: