469,601 Members | 2,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

Need help with substring match

Rob
I've just about beat my brains all the way out on this one. Is anyone
up to the amazingly ridiculous challenge of figuring this one out?
Here's the code, followed by my question:

<script type="text/javascript">
function readCookie(valuename)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
var nameAndValue = new RegExp( valuename + "=[a-z0-9]*" );
the_cookie = the_cookie.match( nameAndValue );

// so far so good, I now have "name=value" stored in the_cookie

var onlyEqualsAndValue = new RegExp( "=[a-z]*" );
the_cookie = the_cookie.match( onlyEqualsAndValue );
alert(the_cookie);
}
</script>

Why doesn't the second call to match() give me just "=value"? The
first regex works flawlessly, but the second one fails for no reason
that I can see--and in fact the alert only pops up if I don't attempt
to make the second call to match(). WTF?

TIA,
Rob

Mar 9 '07 #1
2 1656
The match() method returns an array, rather than a string. The
matching string is contained in the 0 index of the array. So the line:

the_cookie = the_cookie.match( nameAndValue );

Returns an array into "the_cookie". However, when you later execute:

the_cookie = the_cookie.match( onlyEqualsAndValue );

You are treating "the_cookie" as a string. This line should actually
read:

the_cookie = the_cookie[0].match( onlyEqualsAndValue );

That is, you call the match() method on the string contained within
the first index of the "the_cookie" array.

However, you could simply use the following code to get your value:

var nameAndValue = new RegExp( valuename + "=([a-z0-9]*)" );
var matches = the_cookie.match( nameAndValue );
alert(matches[1]);

matches[0] holds the entire matching string, matches[1] holds the
first parenthesised substring in the matching string.

wp.

Mar 9 '07 #2
Rob
On Mar 9, 6:55 am, "wisestpotato" <wisestpot...@googlemail.comwrote:
The match() method returns an array, rather than a string. The
matching string is contained in the 0 index of the array. So the line:

the_cookie = the_cookie.match( nameAndValue );

Returns an array into "the_cookie". However, when you later execute:

the_cookie = the_cookie.match( onlyEqualsAndValue );

You are treating "the_cookie" as a string. This line should actually
read:

the_cookie = the_cookie[0].match( onlyEqualsAndValue );

That is, you call the match() method on the string contained within
the first index of the "the_cookie" array.

However, you could simply use the following code to get your value:

var nameAndValue = new RegExp( valuename + "=([a-z0-9]*)" );
var matches = the_cookie.match( nameAndValue );
alert(matches[1]);

matches[0] holds the entire matching string, matches[1] holds the
first parenthesised substring in the matching string.

wp.

Wow, you're EXACTLY right--thank you so much! I'm ashamed to admit
that if I had RTFM ("JavaScript The Definitive Guide"), I could have
answered my own question.

Thanks again!
Rob

Mar 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Radhika Sambamurti | last post: by
11 posts views Thread by Rolf Barbakken | last post: by
3 posts views Thread by Tim Soliday | last post: by
reply views Thread by Ewart MacLucas | last post: by
5 posts views Thread by JackRazz | last post: by
1 post views Thread by sommarlov | last post: by
15 posts views Thread by lucky | last post: by
4 posts views Thread by Flomo Togba Kwele | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.