Behaviour of string.split with random input

T

Tim Streater

I have this:

splitter = /[ -\/.]/;
dateItems = dateString.split (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.

But it might also be nullstring or any junk the user types in. Now I
find that with the code above, and a null string, .split gives up and I
get a JavaScript error, instead of what I might expect which would be
that the dateItems array contains one item, itself a null string.

Is there something better to use/do in this context? It's completely
stupid to have to parse the string once to check it will not kill
..split, and then do the same again to extract the values I want.

(I get the error with Safari 1.3.1 and Netscape under XP).

Thanks,

-- tim
 
M

Mick White

Tim said:
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.split (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.
[snip]

splitter = /[^\d]/;
dateItems = dateString.split(splitter,3);

Perhaps?
Mick
 
T

Tim Streater

Mick White said:
Tim said:
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.split (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.
[snip]

splitter = /[^\d]/;
dateItems = dateString.split(splitter,3);

Thanks but actually I had been too hasty due to not debugging properly
to see where the script failed. In fact after the .split I had:

Month = dateItems[1];
Month = month.substr(0,3);

which is where it failed with "month has no properties". So I shouldn't
have blamed .split which was not at fault. Now I check that the array
has three items.

-- tim
 
T

Thomas 'PointedEars' Lahn

Tim said:
Thanks but actually I had been too hasty due to not debugging properly
to see where the script failed. In fact after the .split I had:

Month = dateItems[1]; ^
Month = month.substr(0,3); ^ ^
which is where it failed with "month has no properties". [...]
Now I check that the array has three items.

You are sure about your case here?


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 21 Oct 2005 12:27:24, seen in Tim
Streater said:
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.split (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.

But it might also be nullstring or any junk the user types in. Now I
find that with the code above, and a null string, .split gives up and I
get a JavaScript error, instead of what I might expect which would be
that the dateItems array contains one item, itself a null string.

Is there something better to use/do in this context?

Use a RegExp.

Read the newsgroup FAQ; see below; js-valid.htm, js-date4.htm .
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top