Behaviour of string.split with random input

Discussion in 'Javascript' started by Tim Streater, Oct 21, 2005.

  1. Tim Streater

    Tim Streater Guest

    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
    Tim Streater, Oct 21, 2005
    #1
    1. Advertising

  2. Tim Streater

    Mick White Guest

    Tim Streater wrote:

    > 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
    Mick White, Oct 21, 2005
    #2
    1. Advertising

  3. Tim Streater

    Tim Streater Guest

    In article <wf66f.88164$>,
    Mick White <> wrote:

    > Tim Streater wrote:
    >
    > > 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
    Tim Streater, Oct 21, 2005
    #3
  4. Tim Streater wrote:

    > 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
    Thomas 'PointedEars' Lahn, Oct 21, 2005
    #4
  5. JRS: In article <>,
    dated Fri, 21 Oct 2005 12:27:24, seen in news:comp.lang.javascript, Tim
    Streater <> posted :
    >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 .

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Oct 22, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    434
    Larry I Smith
    May 29, 2005
  2. globalrev
    Replies:
    4
    Views:
    746
    Gabriel Genellina
    Apr 20, 2008
  3. Sam Kong
    Replies:
    5
    Views:
    231
    Rick DeNatale
    Aug 12, 2006
  4. Stanley Xu
    Replies:
    2
    Views:
    593
    Stanley Xu
    Mar 23, 2011
  5. VK
    Replies:
    15
    Views:
    1,126
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page