jquery find and replace last parameter from a url

javascript, jquery

Solution

Try this `:)` http://jsfiddle.net/hE6MC/

OR http://jsfiddle.net/YwG6b/

API: http://www.w3schools.com/jsref/jsref_lastindexof.asp

BTW I knw about `:)` http://w3fools.com

code

url = "/view/album/4/photo/1"
var value = url.substring(url.lastIndexOf('/') + 1);

alert(value);

url = url.replace(value, 'hulk')
alert("new url => " + url);​

another way

var str = '/view/album/4/photo/1';
var i = str.lastIndexOf('/');
if (i != -1) {
    str = str.substr(0, i) + "/new stuff";
}

alert(str);
​

Problem

Hi first of all I've already looked around but there is nothing that matched what I want to happen, so I'm asking if anyone can help me with this. I have a URL "/view/album/4/photo/1" and i want to remove the last parameter and replace it with something. What i want to do is to find the last "/" and remove the number after it and replace it with a new value. It's easy with php but i don't know how to do it in jquery since I just started learning it. So I'd really appreciate anyone who can help. :) By the way this is my code in getting the url: action = $(form).attr('action'); // "/view/album/4/photo/1" I don't know what to do with the variable to change the value of the last param

Original source