Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  22] [ 4]  / answers: 1 / hits: 26991  / 10 Years ago, fri, august 15, 2014, 12:00:00

I am trying to figure out how to get the second to last index of a character in a string.



For example, I have a string like so:



http://www.example.com/website/projects/2


I currently get the number 2 by using



$(location).attr('href').substring($(location).attr('href').lastIndexOf('/')+1);



But what if I want to get the word projects?



Can anybody help me out with this? Thanks in advance!


More From » javascript

 Answers
1

You can use split method:



var url = $(location).attr('href').split( '/' );
console.log( url[ url.length - 1 ] ); // 2
console.log( url[ url.length - 2 ] ); // projects
// etc.

[#69768] Wednesday, August 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;