How to extract base URL from a string in JavaScript?

How to extract base URL from a string in JavaScript?

I’m trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery).
For example, given something like:
I’d like to get:
Is a regular expression the best bet? If so, what statement could I use to assign the base URL extracted from a given string to a new variable?
I’ve done some searching on this, but everything I find in the JavaScript world seems to revolve around gathering this information from the actual document URL using or similar.


Solution 1:

Edit: Some complain that it doesn’t take into account protocol. So I decided to upgrade the code, since it is marked as answer. For those who like one-line-code… well sorry this why we use code minimizers, code should be human readable and this way is better… in my opinion.

var pathArray = "".split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var url = protocol + '//' + host;

Or use Davids solution from below.

Related:  Is there a good JS shorthand reference out there?

Solution 2:

WebKit-based browsers, Firefox as of version 21 and current versions of Internet Explorer (IE 10 and 11) implement location.origin.

location.origin includes the protocol, the domain and optionally the port of the URL.

For example, location.origin of the URL is

To target browsers without support for location.origin use the following concise polyfill:

if (typeof location.origin === 'undefined')
    location.origin = location.protocol + '//' +;

Solution 3:

Don’t need to use jQuery, just use


Solution 4:

There is no reason to do splits to get the path, hostname, etc from a string that is a link. You just need to use a link

//create a new element link with your link
var a = document.createElement("a");

//hide it from view when it is added"none";

//add it

//read the links "features"

//remove it

You can easily do it with jQuery appending the element and reading its attr.

Solution 5:

var host = location.protocol + '//' + + '/';

Solution 6:

String.prototype.url = function() {
  const a = $('<a />').attr('href', this)[0];
  // or if you are not using jQuery 👇🏻
  // const a = document.createElement('a'); a.setAttribute('href', this);
  let origin = a.protocol + '//' + a.hostname;
  if (a.port.length > 0) {
    origin = `${origin}:${a.port}`;
  const {host, hostname, pathname, port, protocol, search, hash} = a;
  return {origin, host, hostname, pathname, port, protocol, search, hash};


Then :

 //OUTPUT : {host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050", protocol: "http:",hash:"#23",origin:"http://mysite:5050"}

For your request, you need :


Review 07-2017 : It can be also more elegant & has more features

const parseUrl = (string, prop) =>  {
  const a = document.createElement('a'); 
  a.setAttribute('href', string);
  const {host, hostname, pathname, port, protocol, search, hash} = a;
  const origin = `${protocol}//${hostname}${port.length ? `:${port}`:''}`;
  return prop ? eval(prop) : {origin, host, hostname, pathname, port, protocol, search, hash}


// {origin: "http://mysite:5050", host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050"…}

parseUrl('http://mysite:5050/pke45#23', 'origin')
// "http://mysite:5050"


Related:  Remove all spaces between Chinese words with regex