Categories
JavaScript Answers

How to get value of a string after last slash in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *