Steve,
Thanks for the feedback. This was my first jscript program and I didn't
realize that the slashes contained the regular expression without having to
be in quotes. I would have assumed that the regular expression would have
had to be enclosed in a string. Apparently jscript doesn't work that way.
The finished function that seems to work looks like this:
var optionString = decodeURI(argString);
// this will remove the "&" separators and the initial "?"
var optionArray = optionString.split(/^\?|&/);
while(optionArray.length > 0) {
// shift the first string off of the array
var option = optionArray.shift();
var keyPair = option.split("=");
// if we match the key, return it's value
if(keyPair[0] == key) {
return keyPair[1];
}
}
return "";
Thanks again. I appreciate your help.
Ed
[quoted text, click to view] "Steve Fulton" <cerberus40@hotmail.com> wrote in message
news:opsnxpwxkuboz00o@msnews.microsoft.com...
> Edward Mitchell wrote:
>
>> How can I split a string using a regular expression. It's the query
>> string
>> from the URL that I would like to split into separate key=val pairs. The
>> code that I'm using is as follows:
>>
>> var optionString = decodeURI(argString);
>> // this will remove the "&" separators and the initial "?"
>> var optionArray = optionString.split("/\?|&");
>
> That argument *isn't* a regular expression; it's a string that's not
> even a valid regular expression. Regular expression literals are
> delimited with /, strings with " or '.
>
> And your pattern will split the string on *any* ? character, not just
> the leading one. You'll need to anchor the ? to the start of the
> string.
>
> var optionArray = optionString.split(/^\?|&/);
>
> --
> Steve
>
> The palest ink is better than the best memory. -Chinese proverb