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

292
Visualizações
how to replace/ remove a substring if a string contains that substing

I am writing a function that detects if the current URL contains a certain substring. If it contains, then I would like to remove it.

For example,

localhost/4000?ab=2&item=google

localhost/4000?ab=2&item=google123

localhost/4000?ab=2&item=google1233&haha=helpful

My idea is below....but kinda stuck in the process

function changeUrl(item) {

    var currentUrl = window.location.href; 
        if(currentUrl.includes('&item=') ){
        .....
        .....
        return currentUrl 

    }else return; 
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I wouldn't try to manipulate it as a string. JavaSccript has a perfectly good tool to manipulate URLs, and you might as well use it:

str = 'http://localhost/4000?ab=2&item=google1233&haha=helpful';
url = new URL(str);
url.searchParams.delete('item'); // Idempotent call
result = url.toString();
about 4 years ago · Juan Pablo Isaza Relatório

0

In this case, it's better to use URLSearchParams. MDN DOCS

The URLSearchParams interface defines utility methods to work with the query string of a URL.

var url = new URL('https://example.com?foo=1&bar=2');
var params = new URLSearchParams(url.search);

// you can see params by this way
for (let p of params) {
  console.log(p);
}

// if you want to check if some params are exist
console.log(params.has('foo')); // true

// if you want to delete some params
console.log(params.toString());
params.delete('foo');
console.log(params.toString());

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