Try the tldts library:
npm i tldts
import { parse } from "tldts";const domainWithoutSuffix = parse(href).domainWithoutSuffix;switch (domainWithoutSuffix) { case "facebook": // do stuff case "github": // do stuff case "youtube": // do stuff default: // do stuff}