473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jul 23 '05 #1
8 4213
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Csaba Gabor | last post by:
What I'd like to do is to be able to set the font of a textarea element to the same font that another element is using (say, for example, an <INPUT type=text ...> element, but if that's a no go,...
26
by: sgershon | last post by:
Hi. I know this is should be a simple question. I know server-side web-programming, and never needed to use client-side scripting... until now :) I have done so far a little number of scripts...
6
by: Gabriella | last post by:
Hi, I have a textarea, where the user can enter any given string. He can also insert HTML tags, if he/she wishes. Once I obtain the textarea's string as HTML through form.body.innerHTML, I...
2
by: strauchdieb | last post by:
hey everyone i try to figure out how to calculate string width in a textarea while typing in it. the textarea has a fixed start width. when the string gets longer as the textarea width, the...
16
by: Jen | last post by:
Hi. I have this problem that I think should be easy but have been struggling with this for days. I have a list based on a recordset from a database. This list consists of records meeting a certain...
2
by: dennis.sprengers | last post by:
Ik ben bezig met een eigen UBB editor. Als iemand aan het typen is, zorgt CTRL-B voor een \-tag en nogmaals CTRL-B voor een \ tag. Als je eerst een selectie maakt en dan CTRL-B drukt, wordt de...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
3
kendall
by: kendall | last post by:
I'm currently working on a PHP CMS for my school that uses simple forms to update the pages and database. To get it out in use, it will have to be usable by people who don't know that is bold, and...
4
by: wizardry | last post by:
hello - i've created a form that has multiple inserts. it inserts the data fine if i manually parse the data to it but when i use the form to test the inserts it errors out. it errors out at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.