Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quickest way to search and replace in a query string?

0 views
Skip to first unread message

laredotornado

unread,
Nov 11, 2009, 3:39:32 PM11/11/09
to
Hi,

Given the search or query string from a URL ...

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query string,
add it to the existing query string?

Thanks for your help, - Dave

Luuk

unread,
Nov 11, 2009, 3:56:53 PM11/11/09
to

Thomas 'PointedEars' Lahn

unread,
Nov 11, 2009, 3:57:37 PM11/11/09
to
laredotornado wrote:

You could use a keyboard.

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300...@news.demon.co.uk> (2004)

Csaba Gabor

unread,
Nov 12, 2009, 4:06:23 AM11/12/09
to
On Nov 11, 9:39 pm, laredotornado <laredotorn...@zipmail.com> wrote:
> var searchStr = location.search;
>
> How would i write a function, replace(paramName, paramVal), to replace
> the value in the query string belonging to the param name,
> "paramName", or, if there is no "paramName" given in the query string,
> add it to the existing query string?

Untested, but possibly something like:
first protect paramName and paramVal with one
of the encoding functions:
escape(), encodeURI(), or encodeURIComponent()
and then

var re = new RegExp('\\?' + paramName + '=[^?]*');
var newItem = '?' + paramName + '=' + paramVal;
var newSearchStr = (('?'+searchStr)
.replace(re, "") + newItem).substr(1)

Csaba Gabor from Vienna

0 new messages