Reputation: 404
<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?
Upvotes: 0
Views: 103
Reputation: 7035
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 &
:
<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>
Upvotes: 0
Reputation: 7156
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);
Upvotes: 0