Javascript string.replace with regex

Posted by Jimbo on Stack Overflow See other posts from Stack Overflow or by Jimbo
Published on 2011-02-22T07:23:30Z Indexed on 2011/02/22 7:24 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

I want to replace a url querystring parameter with a new value if it already exists or add it on if not.

e.g.

The current url could be:
a. www.mysite.com/whatever.asp?page=5&version=1 OR
b. www.mysite.com/whatever.asp?version=1

I need the resulting url to be www.mysite.com/whatever.asp?page=1&version=1

I suspect I can use string.replace with a regex to do this the most intelligent way but am hoping for a little help with it from someone more experienced with regexs :) Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery