Ronald Fischer wrote:
> I have a server-side JavaScript function returning a string.
> I would like to test wheather or not the string contains the following pattern:
>
> - an equal sign,
> - followed by one or more characters which are neither an ampersand nor an
> equal sign,
> - followed by another equal sign.
>
> That is: A return value of that function of
> X=ABCY=DEF should match, but
> X=ABC&Y=DEF should not match
>
> This is what I came up with:
>
> if(((/=[^&=]+=/).test(get_query_string())) != null)
> {
> // matches
> }
> else
> {
> // does not match
> }
>
> The problem is that the function matches too much. For example, if
> get_query_string() returns "LANG=EN", it matches too, although the
> string contains only a single equal sign!
>
> Any idea of what could be wrong here?
>
> Ronald
I don't know if there's anything wrong with the regex, I haven't gotten that far.
The reason it's matching everything is because RegExp.test() returns a boolean (two
possible values, true or false). It can _never_ return null, so the "else" code
block is _never_ executed, even when test() returns false. You also don't need so
many brackets around stuff.
Change: if(((/=[^&=]+=/).test(get_query_string())) != null)
to: if (/=[^&=]+=/.test(get_query_string()))
....
Now I've had a chance to look at the regex, and it seems right given the criteria
you've specified.
--
| Grant Wagner <>
* 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