# How to Replace Character in String

**URL:** <https://forum.kirupa.com/t/how-to-replace-character-in-string/293015>\
**Category:** web dev\
**Created:** [July 22, 2009, 5:53am UTC](https://forum.kirupa.com/t/how-to-replace-character-in-string/293015 "2009-07-22T05:53:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [July 22, 2009, 5:53am UTC](https://forum.kirupa.com/t/how-to-replace-character-in-string/293015/1 "2009-07-22T05:53:49Z")

</div>

I have some code that grabs the variable value on the end of a query string, and I want to deal with phrases (such as “Great Road Race”) by placing a plus (+) sign between the words (like "Great+Road+Race) and then run a string replace to swap in a blank space. Here’s the existing code:

```auto

<script type="text/javascript">
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0; i<vars.length; i++) {
        var pair = vars*.split("=");
        if (pair[0] == variable) {
            return pair[1];
        } else {
            return "";// Don't display "undefined"
        }
    }
}
// There is additional script below the form
</script> 

```

Links would look like this:

```auto
<a href="event_registration.html?EventName=Great+Road+Race">Register for this Event</a>

```

What’s the best way to do the string replace?
