String manipulation / TEXTAREA input help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yerk5@hotmail.com

    #1

    String manipulation / TEXTAREA input help

    Does js have some way of doing what I want here:

    I want to paste a multiline block of text into a TEXTAREA form field,
    and there is data in the block of text that I want to extract into a
    variable. It's always going to be as the same column and row position
    of the block of text.

    For example, I want to extract the text from the block from say Line 3,
    column 4 through Line 3, column 15 into a variable.

    For an more hands on example, say I paste this into the textarea:

    BLAH BLAH ARRIVAL ETA: 10:00 BLAH BLAH
    BLAH BLAH DEPART ETA: 14:30 BLAH BLAH

    and I want to capture the 2 eta's into 2 seperate variables.

    Is this possible, and if so, can I get a little help on how? Many TIA

  • RobB

    #2
    Re: String manipulation / TEXTAREA input help

    yerk5@hotmail.c om wrote:[color=blue]
    > Does js have some way of doing what I want here:
    >
    > I want to paste a multiline block of text into a TEXTAREA form field,
    > and there is data in the block of text that I want to extract into a
    > variable. It's always going to be as the same column and row position
    > of the block of text.
    >
    > For example, I want to extract the text from the block from say Line[/color]
    3,[color=blue]
    > column 4 through Line 3, column 15 into a variable.
    >
    > For an more hands on example, say I paste this into the textarea:
    >
    > BLAH BLAH ARRIVAL ETA: 10:00 BLAH BLAH
    > BLAH BLAH DEPART ETA: 14:30 BLAH BLAH
    >
    > and I want to capture the 2 eta's into 2 seperate variables.
    >
    > Is this possible, and if so, can I get a little help on how? Many TIA[/color]

    It's hard to tell from your question what the criteria will be for
    finding the data - the row and (or) column number, or the format of the
    strings involved. You need to be really specific about this to suggest
    a reasonably robust method of isolating the appropriate characters. If
    that "ETA:" business is indicative, here's one way -

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/str­ict.dtd">
    <html>
    <head>
    <title>untitled </title>
    <style type="text/css">

    #extractor {
    width: 400px;
    height: 200px;
    }
    input {
    width: 80px;
    }

    </style>
    <script type="text/javascript">

    var test =
    'BLAH BLAH ARRIVAL ETA: 10:00 BLAH BLAH\n' +
    'BLAH BLAH DEPART ETA: 14:30 BLAH BLAH';

    function extract(obj)
    {
    obj.data = [];
    var x, re = /ETA:\s*(\S+)/gi;
    while (x = re.exec(obj.val ue))
    obj.data.push(x[1]);
    alert(obj.data. join('\n'));
    }

    </script>
    </head>
    <body>
    <form>
    <textarea
    id="extractor"
    name="extractor ">
    </textarea>
    <input
    type="button"
    value="paste"
    onclick="extrac tor.value=test" />
    <input type="button"
    value="test"
    onclick="extrac t(extractor)" />
    </form>
    </body>
    </html>

    Populates an array. If that's not it, be more specific. #:-)

    Comment

    • Mick White

      #3
      Re: String manipulation / TEXTAREA input help

      yerk5@hotmail.c om wrote:
      [...][color=blue]
      >
      > For an more hands on example, say I paste this into the textarea:
      >
      > BLAH BLAH ARRIVAL ETA: 10:00 BLAH BLAH
      > BLAH BLAH DEPART ETA: 14:30 BLAH BLAH
      >
      > and I want to capture the 2 eta's into 2 seperate variables.
      >
      > Is this possible, and if so, can I get a little help on how? Many TIA
      >[/color]

      String.match(re gular expresssion)

      You need a regex to weed out the ETA. Something like:

      str= (reference to textfield value);
      ETAs = str.match(/\s(([01][0-9])|(2[0-5])):[0-5][0-9]\s/g);
      var arrive=ETAs[0];
      var depart=ETAs[1];

      Someone may provide a better regex.
      Mick

      Comment

      • yerk5@hotmail.com

        #4
        Re: String manipulation / TEXTAREA input help

        Wow much appreciated. To get specific, the strings I want to extract
        will not actually be prefixed with any delimiting word or phrase, so I
        think maybe trying to string match wont work.

        Here's a sample of what I'd really be using. I'd want to paste (and
        it's going to look horrible if youre not viewing this with a fixed
        width font):

        |---------||-|---|--|----------------|--|-|-|-|----|
        |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
        :---------:--:---:--:----------------:--:-:-:-:----:
        |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
        |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|0960|
        | | | | | | | | | | |

        So the dates and time are what I want to extract out into a var. The
        only thing constant about them will be their position, so I'm thinking
        that woudl be the best approach to capturing them, but I'm lost as to
        how. :(

        Comment

        • Mick White

          #5
          Re: String manipulation / TEXTAREA input help

          yerk5@hotmail.c om wrote:[color=blue]
          > Wow much appreciated. To get specific, the strings I want to extract
          > will not actually be prefixed with any delimiting word or phrase, so I
          > think maybe trying to string match wont work.
          >
          > Here's a sample of what I'd really be using. I'd want to paste (and
          > it's going to look horrible if youre not viewing this with a fixed
          > width font):
          >
          > |---------||-|---|--|----------------|--|-|-|-|----|
          > |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
          > :---------:--:---:--:----------------:--:-:-:-:----:
          > |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
          > |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|0960|
          > | | | | | | | | | | |
          >
          > So the dates and time are what I want to extract out into a var. The
          > only thing constant about them will be their position, so I'm thinking
          > that woudl be the best approach to capturing them, but I'm lost as to
          > how. :(
          >[/color]

          Are the pipes present in the textarea? (|||)
          But it looks like you need a regex for the date and combine it with time
          regex I provided earlier.

          var re=/((0[1-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01]))\/200[56]/g

          a="02/11/2006 and 02/12/2006".match(re) ;
          alert(a.join("\ n"))


          Mick
          Mick

          Comment

          • RobB

            #6
            Re: String manipulation / TEXTAREA input help

            yerk5@hotmail.c om wrote:[color=blue]
            > Wow much appreciated. To get specific, the strings I want to extract
            > will not actually be prefixed with any delimiting word or phrase, so[/color]
            I[color=blue]
            > think maybe trying to string match wont work.
            >
            > Here's a sample of what I'd really be using. I'd want to paste (and
            > it's going to look horrible if youre not viewing this with a fixed
            > width font):
            >
            > |---------||-|---|--|----------------|--|-|-|-|----|
            > |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
            > :---------:--:---:--:----------------:--:-:-:-:----:
            > |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
            > |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|0960|
            > | | | | | | | | | | |
            >
            > So the dates and time are what I want to extract out into a var. The
            > only thing constant about them will be their position, so I'm[/color]
            thinking[color=blue]
            > that woudl be the best approach to capturing them, but I'm lost as to
            > how. :([/color]

            See if this is anywhere in the vicinity...

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/str­­ict.dtd">
            <html>
            <head>
            <title>untitled </title>
            <style type="text/css">

            #extractor {
            width: 440px;
            height: 100px;
            }
            input {
            width: 80px;
            }

            </style>
            <script type="text/javascript">

            function extract(obj)
            {
            var citypos, etapos,
            oData = {}, str, spc, city, date, time,
            line, lines = obj.value.split ('\n');
            for (var l = 0, ls = lines.length; l < ls; ++l)
            {
            line = lines[l];
            citypos = etapos = -1;
            citypos = line.search(/\bcity\b/i);
            etapos = line.search(/\beta\b/i);
            if (citypos > -1 && etapos > -1)
            break;
            }
            for (++l; l < ls; ++l)
            {
            line = lines[l];
            str = line.substring( citypos);
            if (city = str.match(/[a-z]+/i))
            {
            str = line.substring( etapos);
            if (spc = str.indexOf(' '))
            date = str.substring(0 , spc);
            time = str.substring(s pc + 1);
            if (/\d{1,2}\/\d{1,2}\/\d{2,4}/.test(date)
            && (time = time.match(/\d{1,2}:\d{1,2}/)))
            {
            oData[city] = { date: date, time: time };
            }
            }
            }
            return oData;
            }

            function showData(obj)
            {
            var data = extract(obj);
            var str = '';
            for (city in data)
            str += 'city: ' + city +
            '\ndate: ' + data[city]['date'] +
            '\ntime: ' + data[city]['time'] + '\n\n';
            alert(str);
            }

            </script>
            </head>
            <body>
            <form>
            <textarea
            id="extractor"
            name="extractor ">|---------||-|---|--|---------­-------|--|-|-|-|----|
            |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
            :---------:--:---:--:---------­-------:--:-:-:-:----:
            |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
            |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|0960|
            | | | | | | | | | | |</textarea>
            <input type="button"
            value="test"
            onclick="showDa ta(extractor)" />
            </form>
            </body>
            </html>

            (assuming googlegroups doesn't mangle it beyond repair). Not sure what
            you wanted, exactly. This returns an object (oData) whose properties
            (named for the city in question) contain another object with 'date' &
            'time' properties, self-explanatory. Easily rearranged mini-dB. Overdid
            the parsing a bit but it's not entirely clear how reliably formatted
            the pasted string will be.

            Of course, if Mick W. nailed it,
            just
            ignore
            this &
            have a nice day.
            #:=D

            Comment

            • Dr John Stockton

              #7
              Re: String manipulation / TEXTAREA input help

              JRS: In article <1110495144.643 904.198990@z14g 2000cwz.googleg roups.com>
              , dated Thu, 10 Mar 2005 14:52:24, seen in news:comp.lang. javascript,
              yerk5@hotmail.c om posted :
              [color=blue]
              >Here's a sample of what I'd really be using. I'd want to paste (and
              >it's going to look horrible if youre not viewing this with a fixed
              >width font):
              >
              >|---------||-|---|--|----------------|--|-|-|-|----|
              >|City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
              >:---------:--:---:--:----------------:--:-:-:-:----:
              >|CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
              >|INDEPENDE|OR| RNO| |03/11/2005 15:32| |C|N|N|0960|
              >| | | | | | | | | | |
              >
              >So the dates and time are what I want to extract out into a var. The
              >only thing constant about them will be their position, so I'm thinking
              >that woudl be the best approach to capturing them, but I'm lost as to
              >how. :([/color]

              x = "|CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|".
              substring(21,37 )
              x = "|CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|".
              match(/.{21}(.{16})/)[1]

              each give "03/10/2005 17:21". Beware possible FFF date ambiguities in
              differently-configured browsers; dates should be written as 2005/10/03.

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

              • RobB

                #8
                Re: String manipulation / TEXTAREA input help

                RobB wrote:

                (snip)
                [color=blue]
                > See if this is anywhere in the vicinity...[/color]

                All right, street-swept the vicinity a tad. This is cleaner.

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                "http://www.w3.org/TR/html4/str­­­ict.dtd">
                <html>
                <head>
                <title>untitled </title>
                <style type="text/css">

                body {
                font: 11px monospace;
                color: #fff;
                background: #888;
                }
                #extractor {
                text-align: center;
                background: buttonface;
                overflow: hidden;
                }

                </style>
                <script type="text/javascript">

                function extract(obj)
                {
                var citypos, etapos, oData = {},
                str, spc, city, date, time,
                line, lines = obj.value.split ('\n');
                for (var i = 0, ll = lines.length; i < ll; ++i)
                {
                line = lines[i];
                if ((citypos = line.search(/\bcity\b/i)) > -1
                && (etapos = line.search(/\beta\b/i)) > -1)
                {
                var cre = /[a-z]+/i,
                dre = /\d{1,2}\/\d{1,2}\/\d{2,4}/,
                tre = /\d{1,2}:\d{1,2}/;
                break;
                }
                }
                for (++i; i < ll; ++i)
                {
                line = lines[i];
                str = line.substring( citypos);
                if (city = str.match(cre))
                {
                str = line.substring( etapos);
                if ((spc = str.indexOf(' ')) > -1)
                {
                date = str.substring(0 , spc);
                time = str.substring(s pc + 1).match(tre);
                if (dre.test(date) && null != time)
                {
                oData[city] = { date: date, time: time };
                }
                }
                }
                }
                return oData;
                }

                function showData(id)
                {
                var obj = document.getEle mentById(id),
                data = extract(obj),
                arr = ['\n\n'];
                for (city in data)
                arr.push(
                'city: ·', city,
                '·\ndate: ', data[city].date,
                '\ntime: ', data[city].time,
                '\n\n'
                );
                alert(arr.join( ''));
                }

                </script>
                </head>
                <body>
                <textarea id="extractor"
                name="extractor "
                rows="9"
                cols="53">
                |---------||-­|---|--|---------­-------|--|-­|-|-|----|
                |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
                :---------:--:---:--:---------­­-------:--:-:-:-:----:
                |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
                |NEEDLES |CA|LAX| |03/11/2005 05:00| |C|L|L|0960|
                |RENO |NV|RNO| |03/11/2005 10:44| |W|U|J|1234|
                |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|2579|
                |LOGAN |UT|SLK| |03/12/2005 02:09| |K|H|B|8873|
                | | | | | | | | | | |
                </textarea>
                <span style="cursor:p ointer;" onclick="showDa ta('extractor') ">
                &rarr; click</span>
                </body>
                </html>

                [watch for google-grief]

                Comment

                • Dr John Stockton

                  #9
                  Re: String manipulation / TEXTAREA input help

                  JRS: In article <KV5Yd.87955$H0 5.69408@twister .nyroc.rr.com>, dated
                  Fri, 11 Mar 2005 00:48:42, seen in news:comp.lang. javascript, Mick White
                  <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
                  >yerk5@hotmail. com wrote:[/color]
                  [color=blue][color=green]
                  >> |---------||-|---|--|----------------|--|-|-|-|----|
                  >> |City |St|Exc|SC|Eta 21:11 hrs |Cn|C|P|D|Rte |
                  >> :---------:--:---:--:----------------:--:-:-:-:----:
                  >> |CHINO |CA|ONT| |03/10/2005 17:21|Y |N|C|N|0000|
                  >> |INDEPENDE|OR|R NO| |03/11/2005 15:32| |C|N|N|0960|
                  >> | | | | | | | | | | |
                  >>
                  >> So the dates and time are what I want to extract out into a var[/color][/color]
                  [color=blue]
                  >Are the pipes present in the textarea? (|||)
                  >But it looks like you need a regex for the date and combine it with time
                  >regex I provided earlier.
                  >
                  >var re=/((0[1-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01]))\/200[56]/g[/color]

                  ISTM grossly unlikely that any other field will look anything like date-
                  and-time; so /\d\d\/\d\d\/\d{4} \d\d:\d\d/ will pick it up reliably -
                  and will not fail when 2007 arrives. In fact, /([0-9/ :]{16})/ will do.

                  Beware of pre-over-validation when selecting; ISTM better to be willing
                  to detect the most plausible field for date and time, even if it is not
                  quite on-spec - for example, the day might be given as 3 rather than 03
                  if the data is manually input. Since it is the fifth field that is
                  wanted,
                  x = "|CHINO |CA|ONT| |03/10/2505 17:21|Y |N|C|N|0000|".
                  split('|')[5]
                  might be appropriate (but take care with split if the divider starts the
                  string).

                  If the date/time is wanted for calculation, it should be validated for
                  month-length in the usual manner - the string can be liberally broken up
                  with .split(/\D+/) .

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