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,504
  • 3
  • 31
  • 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

8 Answers8

47

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

window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
Brandon McKinney
  • 1,392
  • 10
  • 9
19

Non-regex.

var link = document.location.href.split('/');
alert(link[3]);
Alex Emilov
  • 1,163
  • 3
  • 17
  • 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
  • 16,172
  • 11
  • 86
  • 115
3

Try with:

var url = 'http://mysite.com/section-with-dashes/';
var section = url.match(/^http[s]?:\/\/.*?\/([a-zA-Z-_]+).*$/)[0];
hsz
  • 143,040
  • 58
  • 252
  • 308
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
  • 4,997
  • 4
  • 21
  • 18
1

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,232
  • 2
  • 10
  • 12
0
$url = 'http://mysite.com/section/subsection';

$path = parse_url($url, PHP_URL_PATH);

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

$first_part = $components[0];
Marc B
  • 348,685
  • 41
  • 398
  • 480
  • 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
  • 4,804
  • 9
  • 56
  • 64