Removing plus signs from URL query string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gene Kelley

    Removing plus signs from URL query string

    This has got to be an easy one, but I'm just not getting it.

    The following function (below) returns name=value pairs from URL queries
    (GET). All is working as expected, but I am trying to figure out how to
    remove the + (plus signs) from the values that contain spaces.

    I'm trying to use the String Object's replace() method to accomplish this.

    This is what I've tried so far along with the results:

    value.replace(' +', ' ') = first and only first occurrence of + is
    replaced

    value.replace(' +'/g, ' ') = error: "g is not defined"

    value.replace(/ /g, ' ') = doesn't replace any + with a space

    value.replace(' '/g, ' ') = error: "g is not defined"

    value.replace(/' '/g, ' ') = doesn't replace any + with a space

    function getArgs() {
    var args = new Object();
    var query = location.search .substring(1); // Get query string
    var pairs = query.split("&" ); // Break at ampersand

    for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('='); // Look for "name=value "
    if (pos == -1) continue; // If not found, skip
    var argname = pairs[i].substring(0,po s); // Extract the name
    var value = pairs[i].substring(pos+ 1); // Extract the value
    value = decodeURICompon ent(value); // Decode it, if needed
    value = value.replace(/+/g, ' '); // replace plus with space
    args[argname] = value; // Store as a property
    }
    return args;
    }


    I'm running out of ideas. ?? Can anyone guide me in the right direction?

    Thanks all...


    --
    ------------------------------------
    Gene Kelley
    Villa Ridge, Missouri, USA
  • Bjoern Hoehrmann

    #2
    Re: Removing plus signs from URL query string

    * Gene Kelley wrote in comp.lang.javas cript:
    >value.replac e(/ /g, ' ') = doesn't replace any + with a space
    Well of course it does not, you are not searching for "+" at all. Use

    value.replace(/\+/g, ' ');

    instead.
    --
    Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

    Comment

    Working...