Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Created September 25, 2023 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/e43d2140d812a756b4f3f01e84e9c3de to your computer and use it in GitHub Desktop.
Save DarrenSem/e43d2140d812a756b4f3f01e84e9c3de to your computer and use it in GitHub Desktop.
hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
// hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
const hostnameRoot = hostname => String(hostname ?? "")
.toLowerCase()
.replace(
/^(m(ob(i(le)?)?)?|ww(w?\d|w))\.(.+?\..+)/,
"$6"
);
console.log([
hostnameRoot("m.youtube.com") === "youtube.com",
hostnameRoot("moB.youtube.com") === "youtube.com",
hostnameRoot("mobI.youtube.com") === "youtube.com",
hostnameRoot("mobilE.youtube.com") === "youtube.com",
hostnameRoot("wwW.youtube.com") === "youtube.com",
hostnameRoot("www1.youtube.com") === "youtube.com",
hostnameRoot("ww2.youtube.com") === "youtube.com",
hostnameRoot("ww.youtube.com") === "ww.youtube.com",
hostnameRoot("youtubE.com") === "youtube.com",
hostnameRoot("immobile.youtube.com") === "immobile.youtube.com",
hostnameRoot("mo.youtube.com") === "mo.youtube.com",
hostnameRoot("mobil.youtube.com") === "mobil.youtube.com",
hostnameRoot("m.com") === "m.com",
hostnameRoot("mobile.com") === "mobile.com",
hostnameRoot("www.com") === "www.com",
hostnameRoot() === "",
hostnameRoot(null) === "",
hostnameRoot(0) === "0",
hostnameRoot([]) === "",
hostnameRoot({}) === "[object object]",
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment