Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda