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

Converting string to an Array: Easy way?

P: n/a
str="West Ham 4 Chelsea 3"

Ideally to
["West Ham",4,"Chelsea",3]

but the following is OK:
["West Ham","4","Chelsea","3"]

The problem of course is the space that sometimes is in the team name.

ARR=str.replace(/([a-z]) ([A-Z])/g,"$1_$2").split(",");

Which replaces the troublesome space with an underscore. I can then
remove the underscore, but I'm looking for a more elegant solution.
??
Mick
Jan 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Lee wrote:
mick white said:
str="West Ham 4 Chelsea 3"

Ideally to
["West Ham",4,"Chelsea",3]

but the following is OK:
["West Ham","4","Chelsea","3"]

The problem of course is the space that sometimes is in the team name.

ARR=str.replace(/([a-z]) ([A-Z])/g,"$1_$2").split(",");

Which replaces the troublesome space with an underscore. I can then
remove the underscore, but I'm looking for a more elegant solution.
??

It's called a manual:

http://docs.sun.com/source/816-6408-...ng.htm#1194452

That doesn't help, I'm afraid.
Thanks.
Mick

Jan 24 '06 #2

P: n/a
mick white <mi**@mickweb.com> writes:
str="West Ham 4 Chelsea 3"

Ideally to
["West Ham",4,"Chelsea",3]

but the following is OK:
["West Ham","4","Chelsea","3"]


The immediate idea is:
str.split(/\s+(\d+)\s*/g)
except that it gives an extra empty string at the end, and that
it doesn't work in IE. The latter could be disqualifying :).

A more elaborate way is:
---
function footballSplit(str) {
var res = [];
var re = /([a-z ]+)\s(\d+)\s*/ig;
for(var match; match = re.exec(str);) {
res.push(match[1],Number(match[2]));
}
return res;
}
---
It expects team names to contain only spaces and letters. If that's not
correct, the regexp should be modified.

Perhaps it would even be a better result to change the res.push line
to
res.push({team: match[1], points: Number(match[2])});
i.e., instead of the list
["West Ham",4,"Chelsea",3"]
you get a list of objects with two properties: team and points
[{team: "West Ham", points: 4},
{team: "Chelsea", points: 3}]
But that depends on how it is to be used.
Even more feature abusing, you can pass a function to replace, and
have it do the looping for you - and then not do any replaceing
anyway:

function footballSplit2(str) {
var res = [];
str.replace(/([a-z ]+)\s+(\d+)\s*/gi, function(m,m1,m2) {
res.push(m1,Number(m2));
});
return res;
}

It works in modern browsers and IE, but it might fail in older
browsers like IE4 (haven't checked, though).
/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.'
Jan 25 '06 #3

P: n/a
Lasse Reichstein Nielsen wrote:
mick white <mi**@mickweb.com> writes:
str="West Ham 4 Chelsea 3"

Ideally to
["West Ham",4,"Chelsea",3]


The immediate idea is:
str.split(/\s+(\d+)\s*/g)
except that it gives an extra empty string at the end, and that
it doesn't work in IE. The latter could be disqualifying :).

A more elaborate way is:
---
function footballSplit(str) {
var res = [];
var re = /([a-z ]+)\s(\d+)\s*/ig;
for(var match; match = re.exec(str);) {
res.push(match[1],Number(match[2]));
}
return res;
}
---
It expects team names to contain only spaces and letters. If that's not
correct, the regexp should be modified.

Perhaps it would even be a better result to change the res.push line
to
res.push({team: match[1], points: Number(match[2])});
i.e., instead of the list
["West Ham",4,"Chelsea",3"]
you get a list of objects with two properties: team and points
[{team: "West Ham", points: 4},
{team: "Chelsea", points: 3}]
But that depends on how it is to be used.
Even more feature abusing, you can pass a function to replace, and
have it do the looping for you - and then not do any replaceing
anyway:

function footballSplit2(str) {
var res = [];
str.replace(/([a-z ]+)\s+(\d+)\s*/gi, function(m,m1,m2) {
res.push(m1,Number(m2));
});
return res;
}

It works in modern browsers and IE, but it might fail in older
browsers like IE4 (haven't checked, though).

Thanks L, your second example is a revelation to me...

Mick
Jan 25 '06 #4

P: n/a
Lasse Reichstein Nielsen wrote:
mick white <mi**@mickweb.com> writes:
str="West Ham 4 Chelsea 3"

Ideally to
["West Ham",4,"Chelsea",3]

but the following is OK:
["West Ham","4","Chelsea","3"]
The immediate idea is:
str.split(/\s+(\d+)\s*/g)
except that it gives an extra empty string at the end, and that
it doesn't work in IE. The latter could be disqualifying :).


[...]
It works in modern browsers and IE, but it might fail in older
browsers like IE4 (haven't checked, though).


For older browsers (as well as newer) you may want to give

str.match(/\d+|[a-z]+\s*[a-z]+/ig);

a try ('match' is JavaScript 1.2).

../rh

Jan 26 '06 #5

P: n/a
ro********@gmail.com wrote:
For older browsers (as well as newer) you may want to give

str.match(/\d+|[a-z]+\s*[a-z]+/ig);

a try ('match' is JavaScript 1.2).


I should have pointed out that RegExp, single char names would not be
included (but it should be easy to adjust, if that's a requirement).

../rh

Jan 26 '06 #6

P: n/a
ro********@gmail.com wrote:
ro********@gmail.com wrote:
For older browsers (as well as newer) you may want to give

str.match(/\d+|[a-z]+\s*[a-z]+/ig);

a try ('match' is JavaScript 1.2).

I should have pointed out that RegExp, single char names would not be
included (but it should be easy to adjust, if that's a requirement).


That's not a requirement, Ron, thanks.
Mick
Jan 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.