Regexp

P

Phat G5 (G3)

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&blah3=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
 
E

Evertjan.

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&blah3=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)
 
E

Evertjan.

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&blah3=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)
 
P

Phat G5 (G3)

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&blah3=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
 
M

Michael Winter

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
 
S

Steffan

Michael said:
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

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
 
M

Michael Winter

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
 
T

Thomas 'PointedEars' Lahn

Michael said:
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
 
S

Steffan

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
 
M

Matt Kruse

Evertjan. said:
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=[^&]*/,'')
 
E

Evertjan.

Matt Kruse wrote on 26 dec 2005 in comp.lang.javascript:
Evertjan. said:
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=hello'
str = str.replace(new RegExp('[&?]' + myItem + '=[^&]*'), '');
alert(str)
 
T

Thomas 'PointedEars' Lahn

Matt said:
Evertjan. said:
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
 
E

Evertjan.

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('\')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top