21

I need to get the first word after slash in a url in javascript, I assume using a regex would be ideal.

Here's an idea of what the URLs can possibly look like :

In bold is what I need the regex to match for each scenario, so basically only the first portion after the slash, no matter how many further slashes there are.

I'm at a complete loss here, appreciate the help.

dom
  • 6,702
  • 3
  • 30
  • 35
  • 4
    Technically, the first word after a slash in your examples is 'mysite'... What you want is the first part of the url's path component. – Marc B Apr 20 '11 at 19:20

9 Answers9

48

JavaScript with RegEx. This will match anything after the first / until we encounter another /.

window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
Brandon McKinney
  • 1,412
  • 11
  • 9
25

Non-regex.

var link = document.location.href.split('/');
alert(link[3]);
Alex Emilov
  • 1,243
  • 4
  • 19
  • 25
10

Exploding an url in javascript can be done using the official rfc2396 regex:

var url = "http://www.example.com/path/to/something?query#fragment";
var exp = url.split(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);

This will gives you:

["", "http:", "http", "//www.example.com", "www.example.com", "/path/to/something", "?query", "query", "#fragment", "fragment", ""]

Where you can, in your case, easily retrieve you path with:

const path = exp[5];

And therefore the first word after the path using:

const rootPath = path.split('/')[1];
Community
  • 1
  • 1
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
3

Try with:

var url = 'http://mysite.com/section-with-dashes/';
var section = url.match(/^http[s]?:\/\/.*?\/([a-zA-Z-_]+).*$/)[0];
hsz
  • 148,279
  • 62
  • 259
  • 315
2

Here is the quick way to get that in javascript

var urlPath = window.location.pathname.split("/");
if (urlPath.length > 1) {
  var first_part = urlPath[1];
  alert(first_part); 
}
Ady Ngom
  • 1,284
  • 2
  • 10
  • 12
1

My regex is pretty bad, so I will improvise with a less efficient solution :P

// The first part is to ensure you can handle both URLs with the http:// and those without

x = window.location.href.split("http:\/\/")
x = x[x.length-1];
x = x.split("\/")[1]; //Result is in x
Fareesh Vijayarangam
  • 5,037
  • 4
  • 23
  • 18
0
$url = 'http://mysite.com/section/subsection';

$path = parse_url($url, PHP_URL_PATH);

$components = explode('/', $path);

$first_part = $components[0];
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 4
    For some reason, I'm thinking that the JavaScript interpreter won't like your PHP solution. – Jim Mischel Apr 20 '11 at 19:48
  • 1
    After blinking a few times, you're probably right... But since there's a lot of people who think Javascript can execute on the server, I can pretend that PHP can execute on the client :) – Marc B Apr 20 '11 at 19:55
0

In case you want to get what is after the first forward slash (included) you can do like so:

const linkUrl = pathname.replace(/^(.*\/)/, '$1')

like for http://localhost:3000/dashboard/dataExploration will return /dashboard/dataExploration

Note this will help you to change the active link element according to the location pathname in react app for ex :).

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
0
string.split('://')[1].split('/')[1];
  • 3
    Welcome to Stack Overflow! Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Aug 16 '22 at 09:36