You can remove last '/' and trailing string without using split function. Just modify the Regex a bit.
const url = 'https://example.com/'; console.log(url.replace(/^(?:https?:\/\/)?(?:www\.)?|\/.*$/i, ""));