since the referrer is a url in string form, we can use the URL class to turn it into an URL instance:
const referrerUrl = new URL(document.referrer);
console.log(referrerUrl.hostname);
Fiddle with google search urk as example: https://jsfiddle.net/d8oyw7u6/