Need help with substring match

R

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
 
W

wisestpotato

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.
 
R

Rob

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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top