Create array from string with different delimiters

A

aliensite

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
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

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
 
B

Brian Genisio

aliensite said:
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


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);
 
A

aliensite

Brian Genisio said:
aliensite said:
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


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);


Thanks!
I also found this link helpfull:
http://tinyurl.com/366sy
at www.breakingpar.com.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Dr John Stockton said:
JRS: In article <[email protected]>, seen
in aliensite <[email protected]> posted at
Mon, 16 Feb 2004 11:54:46 :-
A = S.split(/[^0-9\.]/)

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.


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 -

<QUOTE>
Example 2. Consider the following script:

<SCRIPT LANGUAGE="JavaScript1.2">
str="She sells seashells \nby the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>

Using LANGUAGE="JavaScript1.2", this script produces

"She", "sells", "seashells", "by", "the", "seashore"

Without LANGUAGE="JavaScript1.2", this script splits only on single
space characters, producing

"She", "sells", , , , "seashells", "by", , , "the", "seashore"

</QUOTE>



ECMA 262 explains .split in impenetrable detail.


OP: Use the +.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top