By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Removing plus signs from URL query string

P: n/a
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,pos); // Extract the name
var value = pairs[i].substring(pos+1); // Extract the value
value = decodeURIComponent(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
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* Gene Kelley wrote in comp.lang.javascript:
>value.replace(/ /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:bj****@hoehrmann.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/
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.