1

Like

?a=1&b=2&c=3

I only want to change b=6 while keep the other things the same, how to do it?

1
  • Do you want to change URL variable?? Commented Feb 14, 2011 at 6:06

6 Answers 6

2

Following function if you have to replace any b=XXX with b=newBValue

function ReplaceB(strQuery,newBValue)
{
var idxStart= strQuery.indexOf("b=")
if(idxStart<0)
 return; // b= not found, nothing to change
var idxFin=strQuery.substr(0,idxStart).indexOf("&");
var newQuery;
if(idxFin<0)
newQuery = strQuery.substr(0,idxStart) + "b="+newBValue
 else
newQuery = strQuery.substr(0,idxStart) + "b="+newBValue+strQuery.substr(idxStart+idxFin)
return newQuery;
}
Sign up to request clarification or add additional context in comments.

Comments

1

here a small function to do this:

jQuery.replaceURLParam = function (oldURL, replaceParam, newVal) {
var iStart = oldURL .indexOf(replaceParam  + '=');
var iEnd = oldURL .substring(iStart + 1).indexOf('&');
var sEnd = oldURL .substring(iStart + iEnd + 1);
var sStart = oldURL .substring(0, iStart);

var newURl = sStart + replaceParam + '=' + newVal;

if (iEnd > 0) {
    newURl += sEnd;
}

return newURl;

}

document.body.innerHTML = jQuery.replaceURLParam('www.foo.com?a=1&b=2&c=3', 'b', 6) ;

demo: http://jsfiddle.net/3rkrn/

Comments

1
var queryStr = "?a=1&b=2&c=3";
var startTag = "b=";
var endTag = "&";
var index1 = queryStr.indexOf(startTag) + startTag.length;
var index2 = queryStr.indexOf(endTag,index1);
var newValue = 23;
queryStr = queryStr.substr(0, index1) +   newValue + queryStr.substr(index2);
alert(queryStr);

See it here : http://jsfiddle.net/aQL8p/

Comments

1
var test = "?a=1&b=2&c=3".replace(/b=2/gi,"b=6");
alert(test);

Example

var test = "?a=1&b=2&c=3".replace("b=2","b=6");
alert(test);

Example

var test = "?a=1&b=2&c=3".split("b=2").join("b=6");
alert(test);

Example


Regardless of Number

Want to change to value of b regardless of number it is already? Use:

var test = "?a=1&b=2&c=3".replace(/b=\d/gi, "b=6");
alert(test);

Example

2 Comments

@alex Why is that? Edit: Ah yes, I see what you mean now; post changed. :P
Only 'cos I guess it looks better, or something :)
1
var queryString = '?a=1&b=2&c=3'.replace(/([?&;])b=2([$&;])/g, '$1b=6$2');

jsFiddle.

(JavaScript regex does not support lookbehinds).

2 Comments

"JavaScript regex does not support lookbehinds" : Should of seen me going from as3 to javascript. I was going crazy wondering why my regex wasn't working correctly!
@Shaz Sounds like fun. You'll also notice I didn't use a positive lookahead. No reason why, maybe symmetry? :P
0

See if this works for you - a more or less universal urlFor:

https://gist.github.com/4108452

You would simply do

newUrl = urlFor(oldUrl, {b:6});

Comments

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.