5

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

vatz88
  • 2,422
  • 2
  • 14
  • 25
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
  • 5
    How about split using `/` and then fetch necessary values and join them. `a=url.split('/'); final=a.slice(a.length - n).join('/')` – Rajesh Feb 21 '17 at 10:51
  • Related: http://stackoverflow.com/questions/42022542/is-possible-to-get-last-path-of-regexp-in-url/42022591#42022591 – Mistalis Feb 21 '17 at 10:59

10 Answers10

6
  • Insteadof using 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]);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

This should work:

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var results = absoluteURL.split('/');
var result = results[results.length-2];
alert(result);
Alexandre Nicolas
  • 1,851
  • 17
  • 19
0

You can use .split() on the window.location.pathname:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

arr.forEach(function(item){
  console.log(item);
});

With specific indexes:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

console.log('last::::',arr[arr.length-1]);
console.log('second last::::', arr[arr.length-2]);

Or subsequent pop() can give you last item of array:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

console.log(JSON.stringify(arr, 0, 0), '<---last::::popped-->',arr.pop());
console.log(JSON.stringify(arr, 0, 0), '<-----second last::::popped--->', arr.pop());
Jai
  • 74,255
  • 12
  • 74
  • 103
0

Sth like this?

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var n = 2 //second last
var arr = absoluteURL.split('/')
console.log(arr[arr.length-n])
Rick Lee
  • 743
  • 2
  • 7
  • 19
0

Just split your URL with /:

var absoluteUrl = "http://stackoverflow.com/users/6262169/vikas-kohli";window.location.pathname;
var splitedUrl = absoluteUrl .split('/');
console.log(splitedUrl);

Then get the element you want in the array.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
0

IndexOf or something like that may not what you need. You can use split instead.

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli";
var partNo = 3;
var parts = absoluteURL.split('/');
alert(parts[parts.length - partNo]);
0

A simple for loop would do the trick.

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"    
var n = absoluteURL.indexOf('/');
absoluteURL = absoluteURL.substring(n+2); // Becasue there are two / first

for(i = 0; i < x ; i++) // x should be such that if it is 1, then only the last string will come, if 2, then the last two strings
{
    n = absoluteURL.indexOf('/');
    absoluteURL = absoluteURL.substring(n+1);
}
Hello Kitty
  • 83
  • 1
  • 1
  • 15
0

Hi if u can get nth Index from user u can use this

 var nthIndex = 2;
 var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"  
 for (i = 0; i < nthIndex; i++) {   

        var n = absoluteURL.lastIndexOf('/');           
            absoluteURL = absoluteURL.substring(0,n);}
alert(absoluteURL);
Caner Tezcan
  • 166
  • 1
  • 6
0

Concatenate string from the index you want after splitting it with /

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli";     
var n = 2; // set the index from where you want to get characters
var arr = absoluteURL.split('/');
var a = [];
for(var i=n,j=0; i>=0; i--,j++){
  a[j] = arr[arr.length-i];
}
console.log(a.join('')); // you can join it with any character
vatz88
  • 2,422
  • 2
  • 14
  • 25
0

We can add a function to do that ourselves.

Add below code snippet:

String.prototype.nIndexOf = function (char, index) {
if (index >= this.split(char).length)
    return -1;
else
    return this.split(char, index).join(char).length;
}

Then you can use this as following:

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var n1 = absoluteURL.nIndexOf('/', 4);
var n2 = absoluteURL.nIndexOf('/', 5);
var result = absoluteURL.substring(n1 + 1, n2);
alert(result);