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

287
Views
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 answers
Answer question

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 Report

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 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!