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

Create array from string with different delimiters

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
JRS: In article <ca**************************@posting.google.com >, seen
in news:comp.lang.javascript, aliensite <al*******@excite.com> posted at
Mon, 16 Feb 2004 11:54:46 :-
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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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 20 '05 #2

P: n/a
aliensite wrote:
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[i]);

Jul 20 '05 #3

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
JRS: In article <ca**************************@posting.google.com >, seen
in news:comp.lang.javascript, aliensite <al*******@excite.com> posted at
Mon, 16 Feb 2004 11:54:46 :-
1,2 ,3, 4 5.0

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.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
Brian Genisio <Br**********@yahoo.com> wrote in message news:<40******@10.10.0.241>...
aliensite wrote:
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[i]);


Thanks!
I also found this link helpfull:
http://tinyurl.com/366sy
at www.breakingpar.com.
Jul 20 '05 #5

P: n/a
JRS: In article <d6**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Tue, 17 Feb 2004 23:49:30 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
JRS: In article <ca**************************@posting.google.com >, seen
in news:comp.lang.javascript, aliensite <al*******@excite.com> posted at
Mon, 16 Feb 2004 11:54:46 :-

1,2 ,3, 4 5.0

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 +.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.