Sometimes, we want to get value of a string after last slash in JavaScript.
In this article, we’ll look at how to get value of a string after last slash in JavaScript.
How to get value of a string after last slash in JavaScript?
To get value of a string after last slash in JavaScript, we can use the string lasrtIndexOf
and substring
methods.
For instance, we write
const str = "foo/bar/test.html";
const n = str.lastIndexOf("/");
const result = str.substring(n + 1);
to get the index of the last '/'
in str
with lastIndexOf
.
Then we call str.substring
with n + 1
to return a substring from str
from index n + 1
to the end of the str
string.
Conclusion
To get value of a string after last slash in JavaScript, we can use the string lasrtIndexOf
and substring
methods.