473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Regexp

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
13 1813
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.