How to get substring after last specific character in JavaScript?

The actual code will depend on whether you need the full prefix or the last slash. For the last slash only, see Pedro's answer. For the full prefix (and a variable PREFIX):

var PREFIX = "test/category/";
str.substr(str.lastIndexOf(PREFIX) + PREFIX.length);

You can use String.slice with String.lastIndexOf:

var str = 'test/category/1';
str.slice(0, str.lastIndexOf('/') + 1);
// => "test/category/"
str.slice(str.lastIndexOf('/') + 1);
// => 1

You can use below snippet to get that

var str = 'test/category/1/4'
str.substring(str.lastIndexOf('/')+1)

Tags:

Javascript