all groups > dotnet jscript > march 2005 >
You're in the

dotnet jscript

group:

How do I use a regular expression with stringVar.split(...)


How do I use a regular expression with stringVar.split(...) Edward Mitchell
3/19/2005 11:04:33 PM
dotnet jscript:
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("/\?|&");

but it doesn't work. If I replace the argument to the split(...) method
with a single "&", I get all the name value pairs but the first key is
disfigured by the "?". I thought the regular expression was a simple look
for either a "?" or a "&". I'd expect a zero length string for the first
string in the output array but I can handle that. My test string for the
argument was: "?Title=XXX&h1Val=Foo&h2Val=Bar&h3Val=AAA". The optionArray
after the split shows up in the debugger as a single array element
containing all the original characters.

Ed
--
Edward E.L. Mitchell
Phone: (239)415-7039
6707 Daniel Court
Fort Myers, FL 33908

Re: How do I use a regular expression with stringVar.split(...) Edward Mitchell
3/20/2005 12:00:00 AM
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]

Re: How do I use a regular expression with stringVar.split(...) name
3/20/2005 2:17:57 AM
"How can I split a string using a regular expression."

YES DOCUMENTATION

" I would like to split into separate key=val pairs"


NOW it gets hairy.

If you do not validate INPUT for name value pairs that are exactly
separated by an equal sign (unicode!!) , not reliably done.

AND :

===============

"var optionArray = optionString.split("/\?|&");"

Here you do not EXPLICITLY create a Regular Expression

As soon as 'your' characters are
1) in the string AND OR
2) in your expression AND OR
3) expressed as variables

..............

Well, I know the wheels of money got a ton of hay.
Live the good life and study hard.





[quoted text, click to view]
Re: How do I use a regular expression with stringVar.split(...) Steve Fulton
3/20/2005 6:51:59 AM
[quoted text, click to view]

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

AddThis Social Bookmark Button