Filippo oretti
Filippo oretti

Reputation: 49823

JS replace params in url when somenthing wrong

i need a function to replace url params like this

users/?id=9&op=0?iu=0&t=9

to

users/?id=9&op=0&iu=0&t=9

can someone show me some piece of code to do that?

Upvotes: 1

Views: 123

Answers (2)

PitaJ
PitaJ

Reputation: 15024

Try this:

function fixurl(url) {
   var urlarray = url.split('?');
   var parsed = urlarray[0] + '?' + urlarray[1] + '&' + urlarray[2];

   return parsed;
}

It turns users/?id=9&op=0?iu=0&t=9 into users/?id=9&op=0&iu=0&t=9.

And users.html/?id=9&op=0?iu=0&t=9 into users.html/?id=9&op=0&iu=0&t=9.

Upvotes: 0

Danilo Valente
Danilo Valente

Reputation: 11352

function fixURL(url){
    return url.replace(/\?/g,'&').replace('&','?');
}
fixURL('users.html?id=9&op=0?iu=0&t=9');

Upvotes: 1

Related Questions