Converting string to an Array: Easy way?

M

mick white

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
 
L

Lee

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-10/string.htm#1194452
 
M

mick white

Lee said:
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-10/string.htm#1194452
That doesn't help, I'm afraid.
Thanks.
Mick
 
L

Lee

mick white said:
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-10/string.htm#1194452
That doesn't help, I'm afraid.

No, no, you're supposed to chastize me for not knowing how to read.
I afraid I had used up today's allotment of patience on a coworker.
Assuming that there are no digits in team names, replaces runs of
digits with that run with commas on each side, then split on commas:

ARR=str.replace(/(\d+)/g,",$1,").split(",");
 
L

Lasse Reichstein Nielsen

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 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
 
M

mick white

Lasse said:
mick white said:
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
 
R

ron.h.hall

Lasse said:
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 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
 
R

ron.h.hall

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
 
M

mick white

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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top