How to remove a pattern in JS Regex?

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

let sites = ['', ''];

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 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., this will work:

const sites = ['', '', ''];

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

    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?


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.