Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda