By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

String manipulation / TEXTAREA input help

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ye***@hotmail.com wrote:
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


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.value))
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="extractor.value=test" />
<input type="button"
value="test"
onclick="extract(extractor)" />
</form>
</body>
</html>

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

Jul 23 '05 #2

P: n/a
ye***@hotmail.com wrote:
[...]

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


String.match(regular 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
Jul 23 '05 #3

P: n/a
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|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. :(

Jul 23 '05 #4

P: n/a
ye***@hotmail.com wrote:
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|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. :(


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
Jul 23 '05 #5

P: n/a
ye***@hotmail.com wrote:
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|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. :(


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(spc + 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|RNO| |03/11/2005 15:32| |C|N|N|0960|
| | | | | | | | | | |</textarea>
<input type="button"
value="test"
onclick="showData(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

Jul 23 '05 #6

P: n/a
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Thu, 10 Mar 2005 14:52:24, seen in news:comp.lang.javascript,
ye***@hotmail.com posted :
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. :(


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.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.
Jul 23 '05 #7

P: n/a
RobB wrote:

(snip)
See if this is anywhere in the vicinity...


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(spc + 1).match(tre);
if (dre.test(date) && null != time)
{
oData[city] = { date: date, time: time };
}
}
}
}
return oData;
}

function showData(id)
{
var obj = document.getElementById(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|RNO| |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:pointer;" onclick="showData('extractor')">
&rarr; click</span>
</body>
</html>

[watch for google-grief]

Jul 23 '05 #8

P: n/a
JRS: In article <KV*******************@twister.nyroc.rr.com>, dated
Fri, 11 Mar 2005 00:48:42, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
ye***@hotmail.com wrote:
|---------||-|---|--|----------------|--|-|-|-|----|
|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

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


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.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.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.