Regex question

M

Mick White

Chris said:
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
 
G

Grant Wagner

Mick said:
Chris said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
M

Mick White

Grant said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top