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

Regexp

P: n/a
I am weak when it comes to regexp but hoped someone might know in this case.
I am trying to take a url like this :

something.lasso?blah=blah&blah2=blah2&sort=hello&b lah3=blah3

And remove the &sort=XXX without hurting the rest of the url. The parameter
to be replaced would be a parameter passed to a function. Here is what I
have so far:

function refresh(item) {
current = document.location.href;
if(current.match(item.name+'='))
//pseudo code here
//current.replace(item.name regexp , '');

return (current + "&" + item.name + "=" + item.value);

This function would be fired like this :

All <input type="radio" name="show" value="all"
onclick="document.location=refresh(this);">
Mine <input type="radio" name="show" value="mine"
onclick="document.location=refresh(this);">
Any suggestions are appreciated!

-S

Dec 26 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Phat G5 (G3) wrote on 26 dec 2005 in comp.lang.javascript:
I am weak when it comes to regexp but hoped someone might know in this
case. I am trying to take a url like this :

something.lasso?blah=blah&blah2=blah2&sort=hello&b lah3=blah3

And remove the &sort=XXX without hurting the rest of the url. The
parameter to be replaced would be a parameter passed to a function.


str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'

alert(str)

str = str.replace(/sort=[^&]*&?/,'')

alert(str)
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 26 '05 #2

P: n/a
Evertjan. wrote on 26 dec 2005 in comp.lang.javascript:
Phat G5 (G3) wrote on 26 dec 2005 in comp.lang.javascript:
I am weak when it comes to regexp but hoped someone might know in this
case. I am trying to take a url like this :

something.lasso?blah=blah&blah2=blah2&sort=hello&b lah3=blah3

And remove the &sort=XXX without hurting the rest of the url. The
parameter to be replaced would be a parameter passed to a function.


str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'

alert(str)

str = str.replace(/sort=[^&]*&?/,'')

alert(str)


Sorry, incorrect, the send button was pressed too soon, try this:

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'

str = str.replace(/&?sort=[^&]*/,'')

alert(str)

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'

str = str.replace(/&?sort=[^&]*/,'')

alert(str)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 26 '05 #3

P: n/a
in article Xn********************@194.109.133.242, Evertjan. at
ex**************@interxnl.net wrote on 12/26/05 8:19 AM:
Evertjan. wrote on 26 dec 2005 in comp.lang.javascript:
Phat G5 (G3) wrote on 26 dec 2005 in comp.lang.javascript:
I am weak when it comes to regexp but hoped someone might know in this
case. I am trying to take a url like this :

something.lasso?blah=blah&blah2=blah2&sort=hello&b lah3=blah3

And remove the &sort=XXX without hurting the rest of the url. The
parameter to be replaced would be a parameter passed to a function.


str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'

alert(str)

str = str.replace(/sort=[^&]*&?/,'')

alert(str)


Sorry, incorrect, the send button was pressed too soon, try this:

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'

str = str.replace(/&?sort=[^&]*/,'')

alert(str)

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'

str = str.replace(/&?sort=[^&]*/,'')

alert(str)

I appreciate this. It almost perfect. My ISP apparently sends fast but
receives SLOW. I saw the response on google over 30 minutes ago. Anyhow, I
have been playing with it and have found a small difficulty. I am trying to
place a variable into the reg exp as you may have seen in my formula. I
tried all sorts of variances like

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'
item="sort";

//str = str.replace(/&?sort=[^&]*/,'')
//str2 = eval("/&?" + item + "=[^&]*/");
str3 = /&?(item)=[^&]*/;
alert(str3);
//str2 = new RegExp(str3, "gi");

str = str.replace(str3,'')

alert(str)
How do I get the evaluated variable into the regexp?

-S
Dec 26 '05 #4

P: n/a
On 26/12/2005 16:27, Phat G5 (G3) wrote:

[snip]
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'
item="sort";

//str = str.replace(/&?sort=[^&]*/,'')
[snip]
How do I get the evaluated variable into the regexp?


str.replace(new RegExp('&?' + item + '=[^&]*'), '');

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 26 '05 #5

P: n/a
Michael Winter wrote:
On 26/12/2005 16:27, Phat G5 (G3) wrote:

[snip]
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'
item="sort";

//str = str.replace(/&?sort=[^&]*/,'')


[snip]
How do I get the evaluated variable into the regexp?


str.replace(new RegExp('&?' + item + '=[^&]*'), '');

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


I am not able to get that to work. It does not evalueate it properly
for some reason. Did you by chance test it out? I tried it in Firefox
and Safari.

-S

Dec 26 '05 #6

P: n/a
On 26/12/2005 17:05, Steffan wrote:
Michael Winter wrote:
[snip]
str.replace(new RegExp('&?' + item + '=[^&]*'), '');


[snip]
I am not able to get that to work. It does not evalueate it properly
for some reason. Did you by chance test it out? I tried it in Firefox
and Safari.


Yes, it works in IE, Firefox, and Opera. Even NN4.

Care to post the code you've been using?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 26 '05 #7

P: n/a
Michael Winter wrote:
On 26/12/2005 16:27, Phat G5 (G3) wrote:
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'
item="sort";

//str = str.replace(/&?sort=[^&]*/,'')
How do I get the evaluated variable into the regexp?


str.replace(new RegExp('&?' + item + '=[^&]*'), '');


Perhaps you meant

str.replace(new RegExp('[&?]' + item + '=[^&]*'), '');
PointedEars
Dec 26 '05 #8

P: n/a
Actually I figured it out. i did an exact copy and paste and didnt do
str=str.replace...

My bad! Thanks for the help on this one. I've had to use google to do
this since my isp's refresh rate on the usnet gropus are a bit slow
today.

-S

Dec 26 '05 #9

P: n/a
Evertjan. wrote:
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'
str = str.replace(/&?sort=[^&]*/,'')


Fails with:

str =
'something.lasso?blah=blah&resort=XXX&blah2=blah2& sort=hello&blah3=blah3'

So, I would recommend:

str = str.replace(/[&?]sort=[^&]*/,'')

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 26 '05 #10

P: n/a
Matt Kruse wrote on 26 dec 2005 in comp.lang.javascript:
Evertjan. wrote:
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'
str = str.replace(/&?sort=[^&]*/,'')
Fails with:

str =
'something.lasso?blah=blah&resort=XXX&blah2=blah2& sort=hello&blah3

=blah3'
So, I would recommend:

str = str.replace(/[&?]sort=[^&]*/,'')


Good work, Mat.

combined with the more general Q,
and because "item" is a reserved word:

str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');

==================

Test:

myItem = 'sort'

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'
str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');
alert(str)

str = 'something.lasso?blah=blah&blah2=blah2&sort=hello'
str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');
alert(str)

str = 'something.lasso?sort=hello&blah2=blah2&asdf=bello '
str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');
alert(str)

str = 'something.lasso?blah=blah&blah2=blah2&qwersort=he llo'
str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');
alert(str)
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 26 '05 #11

P: n/a
Matt Kruse wrote:
Evertjan. wrote:
str = 'something.lasso?blah=blah&blah2=blah2&sort=hello& blah3=blah3'
str = str.replace(/&?sort=[^&]*/,'')


Fails with:

str =
'something.lasso?blah=blah&resort=XXX&blah2=blah2& sort=hello&blah3=blah3'

So, I would recommend:

str = str.replace(/[&?]sort=[^&]*/,'')


I would not, because it would also remove the leading `?' if the `sort'
name-value pair was at the first position, which is why I canceled my other
reply. Try

str = str.replace(/((\?)|&)sort(=[^&]*)?/, '$2');

instead. With a variable that would be

str = str.replace(new RegExp('((\\?)|&)' + item + '(=[^&]*)?'), '$2');

(provided that item does not contain any RegExp special characters.)
PointedEars
Dec 26 '05 #12

P: n/a
Thomas 'PointedEars' Lahn wrote on 26 dec 2005 in comp.lang.javascript:
str = str.replace(/[&?]sort=[^&]*/,'')
I would not, because it would also remove the leading `?' if the
`sort' name-value pair was at the first position, which is why I
canceled my other reply. Try

str = str.replace(/((\?)|&)sort(=[^&]*)?/, '$2');

instead. With a variable that would be

str = str.replace(new RegExp('((\\?)|&)' + item + '(=[^&]*)?'),
'$2');

That gives a ?& for ...?sort=qwe&...

try:

re = new RegExp('&?' + myItem + '(=[^&]*)?')
str = str.replace(re,'').replace(/\?&/,'?');
(provided that item does not contain any RegExp special characters.)


Mmm, yes ....

couldn't we prepare myItem [item is reserved]?
myItem = '\'+myItem.split(''),join('\')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 26 '05 #13

P: n/a
On 2005-12-26, Phat G5 (G3) <no****@noone.com> wrote:
I am weak when it comes to regexp but hoped someone might know in this case.
I am trying to take a url like this :

something.lasso?blah=blah&blah2=blah2&sort=hello&b lah3=blah3

And remove the &sort=XXX without hurting the rest of the url. The parameter
to be replaced would be a parameter passed to a function. Here is what I
have so far:


something like this?

function refresh(item)
{
var s=location.search.replace(/&sort=[^&]*&/,"&sort="+item.value+'&');
location.search=s;
return true;
}

Bye.
Jasen
Dec 26 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.