473,804 Members | 3,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4257
ye***@hotmail.c om 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.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. #:-)

Jul 23 '05 #2
ye***@hotmail.c om 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(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
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|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. :(

Jul 23 '05 #4
ye***@hotmail.c om 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|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. :(


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.c om 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|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. :(


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

Jul 23 '05 #6
JRS: In article <11************ **********@z14g 2000cwz.googleg roups.com>
, dated Thu, 10 Mar 2005 14:52:24, seen in news:comp.lang. javascript,
ye***@hotmail.c om 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.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.
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(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]

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.co m> 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|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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16725
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, then a generic element's font will do OK, too. What's the correct way to do this, please (so that it will also work for IE 6)? The motivation for this is that I have some text on the screen and I want to insert a textarea element between the...
26
4273
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 that work well. But there are two that I am having special difficulties with: 1)
6
1832
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 would like to figure if there's a certain HTML tag inside (like <img src...or any other). I can do it through form.body.value.indexOf("<img") != -1, but I'm looking for a better way that will enable me to extract the element as
2
1622
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 textarea should be made bigger while typing programatically. my only problem is to determine the size of the string in the textarea. i know the font family and font size.
16
2857
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 criteria and at the bottom of this list i have a button that inserts all these records to a´nother table in the database. So long everything's ok. BUT, at the top of this list I have a textarea that the user can write down some text to be put...
2
2906
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 selectie ingesloten door \ en \, net als hier op GoT. Wat ik nu probeer is, om de B-knop uit de toolbar te laten oplichten als de cursor op een woord staat dat omgeven is door B-tags: I'm trying to write my own UBB editor. If a user types...
2
3088
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 textbox value in the main page is undefined ---------------------------------------- here are code main page: ------------------------------------------- <script language="JavaScript"> var thedata; var newwin; var thenumber; function...
3
2701
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 all the rest of it. I have noticed that vBulletin has the best formatting buttons (and no, I'm not talking about the WYSIWYG editor, I know that's way beyond me) so I was thinking of something similar (if I get bold, italic and underlined working, I'm...
4
3057
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 the title/comments table. the 2 tables before are inserted fine. i've used echo $_post variable and the variable is their. but its not being inserted into the database. ...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.