var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"
var n = absoluteURL.lastIndexOf('/');
var result = absoluteURL.substring(n + 1);
//alert(result);
console.log(result);
Here I get the result like 'vikas-kohli' as I am using lastIndexOf.
Now if someone wants to get characters from second last index, or it may be 3rd last index, then how can I get this?
I hope I am able to explain my question well
lastIndexOf('/')
use string split('/') method.var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"
var splittedStr = absoluteURL.split('/');
console.log(splittedStr);
Then get the required element from an array.
var res = splittedStr[splittedStr.length-n]; // n: 1,2,3.. cosnole.log(res);
DEMO
var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"
var splittedStr = absoluteURL.split('/');
console.log(splittedStr[splittedStr.length-2]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With