Behaviour of string.split with random input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Streater

    #1

    Behaviour of string.split with random input

    I have this:

    splitter = /[ -\/.]/;
    dateItems = dateString.spli t (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
  • Mick White

    #2
    Re: Behaviour of string.split with random input

    Tim Streater wrote:
    [color=blue]
    > I have this:
    >
    > splitter = /[ -\/.]/;
    > dateItems = dateString.spli t (splitter, 3);
    >
    > where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.
    >[/color]
    [snip]

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

    Perhaps?
    Mick

    Comment

    • Tim Streater

      #3
      Re: Behaviour of string.split with random input

      In article <wf66f.88164$7b 6.13791@twister .nyroc.rr.com>,
      Mick White <mwhite13BOGUS@ rochester.rr.co m> wrote:
      [color=blue]
      > Tim Streater wrote:
      >[color=green]
      > > I have this:
      > >
      > > splitter = /[ -\/.]/;
      > > dateItems = dateString.spli t (splitter, 3);
      > >
      > > where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.
      > >[/color]
      > [snip]
      >
      > splitter = /[^\d]/;
      > dateItems = dateString.spli t(splitter,3);[/color]

      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

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Behaviour of string.split with random input

        Tim Streater wrote:
        [color=blue]
        > 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];[/color]
        ^[color=blue]
        > Month = month.substr(0, 3);[/color]
        ^ ^[color=blue]
        > which is where it failed with "month has no properties". [...]
        > Now I check that the array has three items.[/color]

        You are sure about your case here?


        PointedEars

        Comment

        • Dr John Stockton

          #5
          Re: Behaviour of string.split with random input

          JRS: In article <tim.streater-584DBF.12272421 102005@individu al.net>,
          dated Fri, 21 Oct 2005 12:27:24, seen in news:comp.lang. javascript, Tim
          Streater <tim.streater@d ante.org.uk> posted :[color=blue]
          >I have this:
          >
          >splitter = /[ -\/.]/;
          >dateItems = dateString.spli t (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?[/color]

          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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...