Splitting a string on [0-9]{2}

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart Gilbert

    Splitting a string on [0-9]{2}

    I'm trying to split a string on every 2 numbers. I presumed the
    following would be fine, but I get nothing.



    var re = /[0-9]{8}/;
    var regex = new RegExp(re);
    if(date.match(r egex)) {
    var dateArray = new Array(4);
    dateArray = date.split(/[0-9]{2}/);

    date = dateArray[1];
    date += "/" + dateArray[2];
    date += "/" + dateArray[3];
    }


    date begins as something like "20040818" and so it matches the first
    regex but then it all goes wrong somewhere. What's the best way to do
    what I'm trying to do?

    Thanks in advance,

    Stuart.
  • oeyvind toft

    #2
    Re: Splitting a string on [0-9]{2}

    Perhaps this will do:


    date = "20040818";

    var arr = [];
    i = 0;
    str = "";

    while (date.length > 0)
    {
    arr[i] = date.substring( 0, 2);
    str += arr[i] + '\n';
    date = date.substring( 2);
    i++;
    }

    alert(str);


    Oeyvind


    --




    "Stuart Gilbert" <stu-astound@better. domain.name> skrev i melding
    news:cfuge7$174 $1@news.astound .net...[color=blue]
    > I'm trying to split a string on every 2 numbers. I presumed the
    > following would be fine, but I get nothing.
    >
    >
    >
    > var re = /[0-9]{8}/;
    > var regex = new RegExp(re);
    > if(date.match(r egex)) {
    > var dateArray = new Array(4);
    > dateArray = date.split(/[0-9]{2}/);
    >
    > date = dateArray[1];
    > date += "/" + dateArray[2];
    > date += "/" + dateArray[3];
    > }
    >
    >
    > date begins as something like "20040818" and so it matches the first
    > regex but then it all goes wrong somewhere. What's the best way to do
    > what I'm trying to do?
    >
    > Thanks in advance,
    >
    > Stuart.[/color]


    Comment

    • Lee

      #3
      Re: Splitting a string on [0-9]{2}

      Stuart Gilbert said:[color=blue]
      >
      >I'm trying to split a string on every 2 numbers. I presumed the
      >following would be fine, but I get nothing.
      >
      >
      >
      >var re = /[0-9]{8}/;
      >var regex = new RegExp(re);
      >if(date.match( regex)) {
      > var dateArray = new Array(4);
      > dateArray = date.split(/[0-9]{2}/);
      >
      > date = dateArray[1];
      > date += "/" + dateArray[2];
      > date += "/" + dateArray[3];
      >}
      >
      >
      >date begins as something like "20040818" and so it matches the first
      >regex but then it all goes wrong somewhere. What's the best way to do
      >what I'm trying to do?[/color]

      Split treats the re as the delimiter. It removes it from the result.
      You don't need to declare the array before you assign it the value
      of a new array.

      var example="200408 18";
      var date=example.re place(/\d\d(\d\d)(\d{2 })(\d\d)/,"$1/$2/$3");

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Splitting a string on [0-9]{2}

        Stuart Gilbert wrote:
        [color=blue]
        > var re = /[0-9]{8}/;
        > var regex = new RegExp(re);
        > if(date.match(r egex)) {
        > var dateArray = new Array(4);[/color]

        Not necessary. JS arrays are sized automatically.
        [color=blue]
        > dateArray = date.split(/[0-9]{2}/);
        > date = dateArray[1];
        > date += "/" + dateArray[2];
        > date += "/" + dateArray[3];[/color]

        String.prototyp e.split() removes the delimiter from the components.
        You would need to join them again, but what previously was removed
        cannot be determined later. Which is why you should use
        [color=blue]
        > }
        >
        >
        > date begins as something like "20040818" and so it matches the first
        > regex but then it all goes wrong somewhere. What's the best way to do
        > what I'm trying to do?[/color]

        date.replace(/(\d{2})/g, "/$1").substr(1 );

        Note that your approach is error-prone as it allows the user to use
        two-digit years (not y2k-proof). You should rather use an (international)
        standard date format using four-digit years, like YYYY-MM-DD. Parsing such
        a format is easy:

        date = date.replace(/^(\d{4})-(\d{2})-(\d{2})$/, "$1/$2/$3");
        or
        date = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date).joi n("/");


        PointedEars

        P.S.: Nice From :)
        --
        Shut up and code...

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Splitting a string on [0-9]{2}

          Stuart Gilbert <stu-astound@better. domain.name> writes:
          [color=blue]
          > I'm trying to split a string on every 2 numbers. I presumed the
          > following would be fine, but I get nothing.[/color]
          [color=blue]
          > dateArray = date.split(/[0-9]{2}/);[/color]

          When you split, you only get what is between the strings that you
          split on. In your case, that's empty strings.

          var dateArray = date.match(/\d\d/g);
          [color=blue]
          > date begins as something like "20040818" and so it matches the first
          > regex but then it all goes wrong somewhere. What's the best way to do
          > what I'm trying to do?[/color]

          If you want to match a date string on the form "yyyymmdd", then
          you might as well do that directly

          var match = date.match(/^(\d{4})(\d\d)( \d\d)$/);
          // match is undefined if format was wrong.
          var date = match[1] + "-" + match[2] + "-" + match[3];

          /L
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Stuart Gilbert

            #6
            Re: Splitting a string on [0-9]{2}

            > date.replace(/(\d{2})/g, "/$1").substr(1 );[color=blue]
            >
            > Note that your approach is error-prone as it allows the user to use
            > two-digit years (not y2k-proof). You should rather use an (international)
            > standard date format using four-digit years, like YYYY-MM-DD. Parsing such
            > a format is easy:[/color]

            The date format has already been checked before I start doing that
            parsing. It's only a small portion of my complete code, there are "else
            if" and an "else" as well. Also, I get the date formatted by vxml and
            there's nothing I can do about that. They should probably use a better
            date format than that, but they don't.

            Thanks a lot for your help though, and everyone else too.

            Stuart.

            Comment

            • Dr John Stockton

              #7
              Re: Splitting a string on [0-9]{2}

              JRS: In article <cfuge7$174$1@n ews.astound.net >, dated Tue, 17 Aug 2004
              19:42:55, seen in news:comp.lang. javascript, Stuart Gilbert <stu-
              astound@better. domain.name> posted :[color=blue]
              >I'm trying to split a string on every 2 numbers. I presumed the
              >following would be fine, but I get nothing.
              >
              >
              >
              >var re = /[0-9]{8}/;
              >var regex = new RegExp(re);
              >if(date.match( regex)) {
              > var dateArray = new Array(4);
              > dateArray = date.split(/[0-9]{2}/);
              >
              > date = dateArray[1];
              > date += "/" + dateArray[2];
              > date += "/" + dateArray[3];
              >}
              >
              >
              >date begins as something like "20040818" and so it matches the first
              >regex but then it all goes wrong somewhere. What's the best way to do
              >what I'm trying to do?[/color]

              By reading the newsgroup FAQ, thoughtfully; see below.

              It points you to a site which includes date input, with/without
              validation.

              If your needs are as you indicate, and no more, then Lasse's answer
              should be all the guidance you need.

              --
              © 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...