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.
Get the value of a String after the last Slash #
To get the value of a string after the last slash, call the substring[]
method, passing it the index, after the last index of a /
character as a parameter. The substring
method returns a new string, containing the specified part
of the original string.
Copied!
const str = '/hello/world/index.html'; const afterLastSlash = str.substring[str.lastIndexOf['/'] + 1]; console.log[afterLastSlash]; // 👉️ index.html
The parameter we passed to the String.substring method is the start index - the index of the first character to be included in the returned string.
We used the String.lastIndexOf method to get the index of the last slash character in the string.
We don't want to include the last slash in the returned string, therefore we increment the index by 1
.
The substring
method does not change the original string, it returns a new string containing a part of the
original string. Strings are immutable in JavaScript.
Note that the lastIndexOf
method returns -1
in case it doesn't find the character in the string.
An alternative, perhaps simpler approach is to use the split
and pop
methods.
Copied!
const str = 'hello/world/index.html'; const afterLastSlash = str.split['/'].pop[]; console.log[afterLastSlash]; // 👉️ index.html
In the code snippet, we first call the
String.split method to split on a /
character.
This returns an array of strings without the /
separator.
Copied!
const str = 'hello/world/index.html'; const splitOnSlash = str.split['/']; console.log[splitOnSlash]; // 👉️ ['hello', 'world', 'index.html']
We then call the Array.pop method which removes and returns the last element of the array.
If you care about performance you would use the 1st approach, even though the difference is negligible, unless you're dealing with huge strings.
Further Reading #
- Get the Substring after a specific Character in JavaScript
- Get the Substring before a specific Character in JavaScript
- Get a Substring between 2 Characters in JavaScript
- Check if String Doesn't include Substring in JavaScript
- Check if String ends with Substring in JavaScript