[Javascript] Re: Javascript probem in HTML Form
Shawn Milo
milo at linuxmail.org
Fri May 21 10:01:33 CDT 2004
Flavio,
Once again, an excellent observation.
It does me a lot of good to have
you look at my code. :o)
I think the problems with:
/(\?|\&)rd=([^\&]*)/
would be:
1. It doesn't match till the end
of the line, so it will not return
ONLY the desired parameter. It will
return the desired parameter plus
any extra characters after it.
2. ([^\&]*) doesn't match
the desired info being at the
end of the line.
Here is a correction on that regex, with
test code below. It matches the value
given to rd= whether it is the first,
last, or somewhere in the middle.
/^.*(\?|\&)rd=([^?&$]*)(\?|&|$).*$/
Shawn
Tested example:
-------------------------
var testString;
var returnVal;
testString = '?something=fred&jack=jill&who=yourmom&rd=FOOT&gray=c0c0c0';
returnVal = testString.replace(/^.*(\?|\&)rd=([^?&$]*)(\?|&|$).*$/, '$2');
alert(returnVal);
testString = '?rd=FOOT&something=fred&jack=jill&who=yourmom&gray=c0c0c0';
returnVal = testString.replace(/^.*(\?|\&)rd=([^?&$]*)(\?|&|$).*$/, '$2');
alert(returnVal);
testString = '?something=fred&jack=jill&who=yourmom&gray=c0c0c0&rd=FOOT';
returnVal = testString.replace(/^.*(\?|\&)rd=([^?&$]*)(\?|&|$).*$/, '$2');
alert(returnVal);
----------------------------
End example
----- Original Message -----
From: Flavio Gomes <flavio at economisa.com.br>
Date: Fri, 21 May 2004 10:47:23 -0300
To: "[JavaScript List]" <javascript at LaTech.edu>
Subject: Re: [Javascript] Re: Javascript probem in HTML Form
> Shawn Milo wrote:
>
> >I modified your style to my own readability preferences. Hope you don't mind.
> >
> >Shawn
> >
> >
> >
> >(...)
> >
> > var returnVal = location.search.replace(/^\?rd=(.*)$/, '$1');
> >
> >
> I suggest a little change, because I believe that "rd" is not always
> coming nor first nor last in location.search:
> I'm trying like crazy to make it work, but I can't o.o
> My idea would be the following:
> replace(/(\?|\&)rd=([^\&]*)/, '$2');
> Searches for a "?" or "&" followed by "rd=" and then get everything
> untill it finds a "&" (or the end of the line).
> But it doesnt worked.. do you know why Shawn?
>
>
> PS.: While writing the mail I got a working one, but, if you know, may you explain why the above one doesn't works?
> replace(/(\?|\&).*rd=([^\&]*).*$/, '$2')
>
> --
> Flavio Gomes
> flavio at economisa.com.br
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
More information about the Javascript
mailing list