How to remove a pattern in JS Regex?

The following code hides all webpages of a certain website and mocks the website.

let sites = ['mako.co.il', 'walla.co.il'];

for (let i = 0; i < sites.length; i++) {
	if (window.location.href.indexOf(sites[i]) != -1 ) {

    alert(` Enough with this ${sites[i]} shit! `);
	}
}

It displays domain.tld this way:

“Enough with this domain.tld shit!”.

How could I strip away the .tld, so the final outcome would be:

“Enough with this domain shit!”.

A /[domain]@.2,/ regex might unmatch tld’s like .com or co.uk and only “domain” will appear on the alert, but I don’t know how to implement such regex to the sites[i] in the confirm.

Do you know?

AFAIK, there’s not an easy way to do this without fully listing out all of the TLDs.

However, if you can guarantee that there won’t be any subdomains (e.g. whatever.mako.co.il), this will work:

const sites = ['mako.co.il', 'walla.co.il', 'sitepoint.com'];

sites.forEach((site) =>{
  const domain = location.host.replace('www.','');

  if(domain.includes(site)){
    const siteName = domain.split('.')[0];
    alert(`Enough with ${siteName} already! `);
  }
});
1 Like

Hi @James_Hibbard !

fully listing out all of the TLDs.

Can you elaborate more on “fully listing” these? An array with these?


.com
.co.uk
.co.il
.net
.org

Elaboration would be most welcome!

Are those the only TLDs you’re checking for?

@James_Hibbard, at the moment, yes.

Well, you could do something similar to what is suggested in this SO thread:

But if you can exclude (or don’t mind) the inclusion of a subdomain, I’d just use the solution I gave you above.

Here is a complete session with a solution:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.