Re: Create array from string with different delimiters
JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
Mon, 16 Feb 2004 11:54:46 :-[color=blue]
>How to create an array if the user enters any of the following formats:
>1 2 3 4 5.0
>1,2,3,4,5.0
>1, 2, 3, 4, 5.0
>
>and if possible
>1,2 ,3, 4 5.0[/color]
var S = <as above>
A = S.split(/[^0-9\.]/)
where the contents of the square brackets represent all characters that
may be present between numbers. You may want to exclude from that plus,
minus, and possibly E and/or X.
Note that you will have an array of Strings
for (j=0;j<A.length ;j++) A[j] = + A[j] // for Numbers
Re: Create array from string with different delimiters
aliensite wrote:
[color=blue]
> How to create an array if the user enters any of the following formats:
> 1 2 3 4 5.0
> 1,2,3,4,5.0
> 1, 2, 3, 4, 5.0
>
> and if possible
> 1,2 ,3, 4 5.0[/color]
Try this
// Testing string
var text = "1,2 ,3, 4 5.0";
// replace all commas with spaces
text = text.replace(/[,]/g, " ");
// replace all space strings of 2 or more with a single space
text = text.replace(/[ ][ ]+/g, " ");
// split into an array based off of spaces
values = text.split(" ");
// test it out
for( i in values )
alert(values[i]);
Re: Create array from string with different delimiters
Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
[color=blue]
> JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
> in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
> Mon, 16 Feb 2004 11:54:46 :-[/color]
[color=blue][color=green]
>>1,2 ,3, 4 5.0[/color][/color]
[color=blue]
> A = S.split(/[^0-9\.]/)[/color]
I think you forgot the +, i.e.,
A = S.split(/[^0-9\.]+/)
Otherwise "3, 4" will be split into three parts, the middle one empty.
/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.'
Re: Create array from string with different delimiters
Brian Genisio <BrianGenisio@y ahoo.com> wrote in message news:<40321139@ 10.10.0.241>...[color=blue]
> aliensite wrote:
>[color=green]
> > How to create an array if the user enters any of the following formats:
> > 1 2 3 4 5.0
> > 1,2,3,4,5.0
> > 1, 2, 3, 4, 5.0
> >
> > and if possible
> > 1,2 ,3, 4 5.0[/color]
>
>
> Try this
>
> // Testing string
> var text = "1,2 ,3, 4 5.0";
>
> // replace all commas with spaces
> text = text.replace(/[,]/g, " ");
> // replace all space strings of 2 or more with a single space
> text = text.replace(/[ ][ ]+/g, " ");
> // split into an array based off of spaces
> values = text.split(" ");
>
> // test it out
> for( i in values )
> alert(values[i]);[/color]
Re: Create array from string with different delimiters
JRS: In article <d68dqq2t.fsf@h otpop.com>, seen in
news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
posted at Tue, 17 Feb 2004 23:49:30 :-[color=blue]
>Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
>[color=green]
>> JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
>> in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
>> Mon, 16 Feb 2004 11:54:46 :-[/color]
>[color=green][color=darkred]
>>>1,2 ,3, 4 5.0[/color][/color]
>[color=green]
>> A = S.split(/[^0-9\.]/)[/color]
>
>I think you forgot the +, i.e.,
> A = S.split(/[^0-9\.]+/)
>Otherwise "3, 4" will be split into three parts, the middle one empty.[/color]
I tested it, without +, on the OP's strings, and noticed no empty
element.
OTOH you are obviously right.
OTTH, in my MSIE4,
A = "3, 4".split(/[^0-9\.]/)
returns [3,4] of .length 2.
A saved Netscape page implies that .split was different (from what?) in
JS1.2 -
Comment