16
"http://something.com:6688/remote/17/26/172"

if I have the value 172 and I need to change the url to 175

"http://something.com:6688/remote/17/26/175"

how can i do that in JavaScript?

Frankie
  • 24,627
  • 10
  • 79
  • 121
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • if 17,26 are static values its pretty easy , you can use indexOf(17.26) and it returns a value , then do substirng(position) – kobe Oct 26 '10 at 16:17

6 Answers6

33
var url = "http://something.com:6688/remote/17/26/172"
url = url.replace(/\/[^\/]*$/, '/175')

Translation: Find a slash \/ which is followed by any number * of non slash characters [^\/] which is followed by end of string $.

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • GeorgeBailey's solution is pretty neat , even if the values changes it will work – kobe Oct 26 '10 at 16:20
  • 1
    Translating the regexp to English--find a string: a / which is not followed by a / which is followed by any number of any characters which is followed by the end of the string. Replace it with "/175". An excellent regexp solution. To use a / in JS regexp, it is escaped as \/ – Larry K Oct 26 '10 at 16:46
  • I added my own explanation which is slightly more accurate. – 700 Software Apr 17 '13 at 13:08
  • 3
    I would also add an optional / before the end so it works both in .../172 and .../172/ So it would be: /\/[^\/]*\/?$/ – Carlos López Marí Oct 18 '20 at 14:01
  • Don't forget that URLs like this exist: `http://something.com/something?value=/page`. The regex in this answer would fail on this URL, as it would update the query param instead of the page URL. There's an easy work around though, just take the query param off the URL before running the regex replace (and put the query param back on the altered URL if needed) – Skeets Sep 30 '22 at 02:13
8

new URL("175", "http://something.com:6688/remote/17/26/172").href

This also works with paths, e.g.

new URL("../27", "http://something.com:6688/remote/17/26/172").href"http://something.com:6688/remote/17/27"

new URL("175/1234", "http://something.com:6688/remote/17/26/172").href"http://something.com:6688/remote/17/26/175/1234"

new URL("/local/", "http://something.com:6688/remote/17/26/172").href"http://something.com:6688/local/"

See https://developer.mozilla.org/en-US/docs/Web/API/URL/URL for details.

7

Split the String by /, remove the last part, rejoin by /, and add the new path

newurl = url.split('/').slice(0,-1).join('/')+'/175'
commonpike
  • 10,499
  • 4
  • 65
  • 58
2

Split the String by / then change the last part and rejoin by /:

var newnumber = 175;
var url = "http://something.com:6688/remote/17/26/172";
var segements = url.split("/");
segements[segements.length - 1] = "" + newnumber;
var newurl = segements.join("/");
alert(newurl); 

Try it!

Adam
  • 43,763
  • 16
  • 104
  • 144
  • don't mind but i think regex replace might be very simple for this kind of pattern replaces – kobe Oct 26 '10 at 16:30
  • @gov, I agree. I regex is simpler and a better solution. The only advantage of my approach is readability – Adam Oct 26 '10 at 16:34
1

Depends on what you want to do.

Actually change browser URL:
If you actually want to push the browser to another URL you'll have to use window.location = 'http://example.com/175'.

Change browser URL hash
If you just want to change the hash you can simply use window.location.hash.

Re-use the URL on links or similar
If you want to reference a URL in links or similar, look into George's answer.

Community
  • 1
  • 1
Frankie
  • 24,627
  • 10
  • 79
  • 121
1

//Alternative way.

var str =  window.location.href;
var lastIndex = str.lastIndexOf("/");
var path = str.substring(0, lastIndex);
var new_path = path + "/new_path";
window.location.assign(new_path);
Skanda Mallappa
  • 111
  • 3
  • 4