473,406 Members | 2,705 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,406 software developers and data experts.

Create array from string with different delimiters

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
5 5035
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: brianshields | last post by:
array("a", "b", "c", "d", "e"); I want to now be able to load "b" into a variable ($var) then print it out print $var thanks!
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
9
by: Steve | last post by:
Hello (and a happy new year) I'm quite new to C++ and have to programm something for school and can't get my head around a couple of things, but at the moment this one is the most important for...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: Anil | last post by:
Hi All, I have a string which has product names, which are seperated by comma. The number of products in the string are random. I want to populate the array using the string, one product per...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
3
by: rupinderbatra | last post by:
Hello everyone, I am using a regular expression to parse a text string into various parts -- for ex: string "How do you do" will be changed to array with all the words and white spaces. I am...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.