2

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!

2 Answers 2

2

I would rather use location.search along with some .splits() and Array.prototype.somehelp.

var s     = location.search.slice(1).split(/&/);
    check = s.some(function(elem) {
        return elem.split(/=/)[0] === 'page';
    });

if(!check) s.push('page=1');

location.href = location.hostname + location.pathname + '?' + s.join('&');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I gave it to Benson as I used his to solve my problem and for the lesson in regexs :P
0

I think the clearest solution would be to write one regex that parses the URL, and then build a URL from there. Here's what I would do:

function urlCleanup(url) {
  var match = /http:\/\/www\.mysite\.com\/whatever.asp\?(page=(\d+))?&?(version=(\d+))?/.exec(url);
  var page = match[2] ? match[2] : "0";
  var version = match[4] ? match[4] : "0";
  return "http://www.mysite.com/whatever.asp?page=" + page + "&version=" + version;
}

var testUrls = [ "http://www.mysite.com/whatever.asp?page=4"
               , "http://www.mysite.com/whatever.asp?version=5"
               , "http://www.mysite.com/whatever.asp?page=4&version=5" ];

for(i in testUrls)
  console.log(urlCleanup(testUrls[i]));

One problem this doesn't handle is having the variables in the opposite order in the url (e.g. ?version=5&page=2). To handle that, it would probably make more sense to use two regexes to search the URL for each parameter, like this:

function urlCleanup(url) {
  var match, page, version;
  match = /version=(\d+)/.exec(url);
  version = match ? match[1] : "0";
  match = /page=(\d+)/.exec(url);
  page = match ? match[1] : "0";

  return "http://www.mysite.com/whatever.asp?page=" + page + "&version=" + version;
}

var testUrls = [ "http://www.mysite.com/whatever.asp?page=4"
               , "http://www.mysite.com/whatever.asp?version=5"
               , "http://www.mysite.com/whatever.asp?version=5&page=2"
               , "http://www.mysite.com/whatever.asp?page=4&version=5" ];

for(i in testUrls)
  console.log(urlCleanup(testUrls[i]));

5 Comments

Max respect :P Let me play with it for a bit now :)
I'm playing with what I wrote here in a console, and it's not quite right, I'll edit in a much cleaner version shortly.
Its actually only the page part that I need to play with. If page parameter exists, replace it with new one, otherwise add it to the end
Oh, crap. My answer is far too complex for what you need then, sorry I misread the question. For that, you just need a subset of my second code block -- let me know if it's not obvious what you need to do.
This helped me solve the problem by using your regex examples, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.