I have a string test/category/1. I have to get substring after test/category/. How can I do that?
8 Answers
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
Comments
A more complete compact ES6 function to do the work for you:
const lastPartAfterSign = (str, separator='/') => {
let result = str.substring(str.lastIndexOf(separator)+1)
return result != str ? result : false
}
const input = 'test/category/1'
console.log(lastPartAfterSign(input))
//outputs "1"