Get value of a string after last slash in JavaScript

242,741

Solution 1

At least three ways:

A regular expression:

var result = /[^/]*$/.exec("foo/bar/test.html")[0];

...which says "grab the series of characters not containing a slash" ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the whole matched string. No need for capture groups.

Live example

Using lastIndexOf and substring:

var str = "foo/bar/test.html";
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);

lastIndexOf does what it sounds like it does: It finds the index of the last occurrence of a character (well, string) in a string, returning -1 if not found. Nine times out of ten you probably want to check that return value (if (n !== -1)), but in the above since we're adding 1 to it and calling substring, we'd end up doing str.substring(0) which just returns the string.

Using Array#split

Sudhir and Tom Walters have this covered here and here, but just for completeness:

var parts = "foo/bar/test.html".split("/");
var result = parts[parts.length - 1]; // Or parts.pop();

split splits up a string using the given delimiter, returning an array.

The lastIndexOf / substring solution is probably the most efficient (although one always has to be careful saying anything about JavaScript and performance, since the engines vary so radically from each other), but unless you're doing this thousands of times in a loop, it doesn't matter and I'd strive for clarity of code.

Solution 2

Try this:

const url = "files/images/gallery/image.jpg";

console.log(url.split("/").pop());

Solution 3

You don't need jQuery, and there are a bunch of ways to do it, for example:

var parts = myString.split('/');
var answer = parts[parts.length - 1];

Where myString contains your string.

Solution 4

var str = "foo/bar/test.html";
var lastSlash = str.lastIndexOf("/");
alert(str.substring(lastSlash+1));

Solution 5

Try;

var str = "foo/bar/test.html";
var tmp = str.split("/");
alert(tmp.pop());
Share:
242,741

Related videos on Youtube

r0skar
Author by

r0skar

meh

Updated on March 24, 2022

Comments

  • r0skar
    r0skar over 2 years

    I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy:

    I have something like this : foo/bar/test.html

    I would like to use jQuery to extract everything after the last /. In the example above the output would be test.html.

    I guess it can be done using substr and indexOf(), but I cant find a working solution.

  • Andrew Jackman
    Andrew Jackman over 12 years
    +1 (for working) pointing out that jQuery is not the be-all end-all solution for javascript issues.
  • Donatas Cereska
    Donatas Cereska over 10 years
    This is a buggy solution, try what happens if you add the slash to the slug? It returns 0. And what happens there is no slug? - returns NaN! wtf? bad code
  • Tom Walters
    Tom Walters over 10 years
    1) The question was related to an input which wasn't null 2) The input was related to inputs ending in file-names (no slashes) 3) The accepted answer is indeed a much better solution, hence the numerous up votes 4) You're welcome to add your own solution
  • wbadart
    wbadart about 9 years
    How would you adapt this to return the string before final slash? (I.e return the name of the parent folder)
  • β.εηοιτ.βε
    β.εηοιτ.βε about 4 years
    This solution is already in the accepted answer, as the third possible solution