473,320 Members | 1,854 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.

Regex question

I need to write a function that will remove a specified parameter from a
URL. For example:

removeParam("param1", "http://mysite.com/mypage.htm?param1=1&param2=2");

would return:

"http://mysite.com/mypage.htm?param2=2"

I'm thinking that string.replace(/regex/, ""); would do the trick, but how
do I construct a correct regex?

I see a problem if the parameter name ("param1") happens to contain any
characters that have a special meaning in a regular expression.
Jul 23 '05 #1
3 2222
Chris wrote:
I need to write a function that will remove a specified parameter from a
URL. For example:

removeParam("param1", "http://mysite.com/mypage.htm?param1=1&param2=2");

would return:

"http://mysite.com/mypage.htm?param2=2"
This would be a candidate for string manipulation rather than using reg
ex. But Reg ex is something like the following:

function removeParam(param,url){
regex=eval("/&?"+param+"=[^&]+&/")
return url.replace(regex,'')
}

This should work with any number of queries, characters inside square
brackets don't need to be escaped, I think.

Mick

I'm thinking that string.replace(/regex/, ""); would do the trick, but how
do I construct a correct regex?

I see a problem if the parameter name ("param1") happens to contain any
characters that have a special meaning in a regular expression.

Jul 23 '05 #2
Mick White wrote:
Chris wrote:
I need to write a function that will remove a specified parameter from a
URL. For example:

removeParam("param1", "http://mysite.com/mypage.htm?param1=1&param2=2");

would return:

"http://mysite.com/mypage.htm?param2=2"
This would be a candidate for string manipulation rather than using reg
ex. But Reg ex is something like the following:

function removeParam(param,url){
regex=eval("/&?"+param+"=[^&]+&/")


eval() is evil().

<url: http://jibbering.com/faq/#FAQ4_40 />

Especially when there is a RegExp object constructor that lets you do the same
thing.

var regex = new RegExp("&?" + param + "=[^&]+&");

Also "var" keyword added to avoid scope issues on the "regex" variable.
return url.replace(regex,'')
}

This should work with any number of queries, characters inside square
brackets don't need to be escaped, I think.

Mick


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #3
Grant Wagner wrote:
Mick White wrote:
This would be a candidate for string manipulation rather than using reg
ex. But Reg ex is something like the following:

function removeParam(param,url){
regex=eval("/&?"+param+"=[^&]+&/")

eval() is evil().


I disagree, especially when "param" is unknown, and may contain escaped
characters.

Mick
<url: http://jibbering.com/faq/#FAQ4_40 />

Especially when there is a RegExp object constructor that lets you do the same
thing.

var regex = new RegExp("&?" + param + "=[^&]+&");

Also "var" keyword added to avoid scope issues on the "regex" variable.

return url.replace(regex,'')
}

This should work with any number of queries, characters inside square
brackets don't need to be escaped, I think.

Mick

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.