Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / JScript / November 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edward Mitchell - 20 Mar 2005 05:04 GMT
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
name - 20 Mar 2005 08:17 GMT
"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.

> 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
[quoted text clipped - 19 lines]
> 6707 Daniel Court
> Fort Myers, FL 33908
Steve Fulton - 20 Mar 2005 12:51 GMT
> 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
[quoted text clipped - 3 lines]
>             // 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(/^\?|&/);

Signature

Steve

The palest ink is better than the best memory. -Chinese proverb

Edward Mitchell - 21 Mar 2005 00:09 GMT
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

>> How can I split a string using a regular expression. It's the query
>> string
[quoted text clipped - 14 lines]
>
>   var optionArray = optionString.split(/^\?|&/);
Tia - 20 Nov 2005 15:38 GMT
START YOUR OWN E-MAIL BUSINESS TODAY and TAKE ADVANTAGE OF THE FOLLOWING:

1.ALL CUSTOMERS PAY YOU IN CASH!!!
2. TOP PART-TIME OWNERS HAVE EARNED $5,000+ IN ONE MONTH!
3. TOP FULL-TIME OWNERS HAVE EARNED $10,000+ IN ONE MONTH!
4. YOU WILL SELL A PRODUCT WHICH COSTS NOTHING TO PRODUCE!
5. YOUR MAIN OVERHEAD IS YOUR TIME!
6. YOUR START UP COSTS ARE LESS THAN $25
7. YOU HAVE MORE THAN 40 MILLION POTENTIAL CUSTOMERS

For additional information please E-mail me at  saita@dodo.com.au
=====================================================

New Software that finds leads for YOU for F.R.E.E.!!!!!
http://www.contactthem.com/cgi-bin/at.cgi?a=316161

---
MAF Anti-Spam ID: 20051113184547I1n0UmZ7

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.