JSFiddle - React, Tailwind, and code Playground
JavaScript
var p = /^(?:http|https)?(?::\/\/)?(?:www\.)?(.*?)(\/.*)?/;
//var p = /^(?:http|https)?(?::\/\/)?(?:www\.)?(.*?)(\/.*)/; //this makes u1 work (removed ? at the end)
var u1 = "http://www.site.com/part1/part2?key=value#blub";
var u2 = "http://site.com";
var u3 = "site.com";
var url = u1; //set wanted url here
var parts = p.exec(url);
var auth = parts[1];
var path = parts[2];
console.log("url: " + url);
console.log("auth: " + auth); //expect: site.com
console.log("path: " + path); //expect: /part1/part2 (or nothing)