Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Replace only last path part in URL not parametres

I can change the last part (in the URL: child) of the URL, but if the URL's parameters exist, these parameters are deleted by this change (?para=1&para=2).

Is there a way to not delete the parameters?

Sample:

https://example.com/sub/child?para=1&para=2

JS:

const str = window.location.href;
const lastIndex = str.lastIndexOf("/");
const path = str.substring(0, lastIndex);
const new_path = path + "/new_child";
window.history.pushState("object or string", "Title", new_path);
window.history.replaceState("object or string", "Title", new_path);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Before replacing the last part you can save the parameters like this

var str = "https://example.com/sub/child?para=1&para=2";
var params = str.split('?')[1]
const lastIndex = str.lastIndexOf("/");
const path = str.substring(0, lastIndex);
const new_path = path + "/new_child?"+params;
console.log(new_path)

Alternatively you can create a function to do this using regex

var str = "https://example.com/sub/child?para=1&para=2";
const new_path = replaceUrlPart(str, "child", "new_child")
console.log(new_path)

function replaceUrlPart(url, partToRemove, partToAdd){
 return url.replace(partToRemove,partToAdd);
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!