0
<script type="text/javascript">
var currstr = "?distance=10&city=Boston&criteria=shoes&size=10";
var selcity = "California"; //This can come from a drop down

var pat = new RegExp([NEEDPATTERN],"gi");
currstr.replace(pat,selcity);
</script>

I need some help replacing city=Boston in currstr with a value user selects from drop down, right now I have hardcoded value in var selcity. I have placed a [NEEDPATTERN] marker for pattern that can be used to achieve this.

Can someone help me with this pattern?

3 Answers 3

1
/City=([^&]+)/gi

should do the trick

Sign up to request clarification or add additional context in comments.

Comments

0

Do you want to replace "Boston" specifically, or do you need to just replace the X in city=X?

For boston, you can just use simple substitution... currstr.replace('Boston', selcity)

For replacing X, you can do: currstr.replace(/city=([^&]+)/, selcity);

1 Comment

Awesome!!. This currstr.replace(/city=([^&]+)/, selcity); works. I wanted to replace any X with new value. Thank you all.!!
0

If it's possible, rather than replacing you should leave city out entirely and tack it on at the end. Then you don't have to deal with regexing which could be more complicated than the current answers if you take into account entities like &amp;:

<script type="text/javascript">
 var currstr = "?distance=10&criteria=shoes&size=10";
 var selcity = "California"; //This can come from a drop down

 currstr += "&city=" + encodeURIComponent(selcity);
</script>

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.