How to remove the tld with regexp

I have this

// Raw
window.onload = function() {
  var str = "";
// 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;

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


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('') // => maichimp
f('') // => google

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

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:
NOTE: they are not in array format, so you’ll have to re-format

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