Created
September 25, 2023 11:22
-
-
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].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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