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

recognize number inner a string's array

P: n/a
if I have an array where the number are write in string format,
how can I recognize when a string is in realty a number?
example
if I have this
"50"
"house"
"light"
"100blue"
"yellow20"
"-100"
"20,5"
how can with a cicle to extract only
50
-100
20,5
?
Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

artev wrote:
if I have an array where the number are write in string format,
how can I recognize when a string is in realty a number?
example
if I have this
"50"
"house"
"light"
"100blue"
"yellow20"
"-100"
"20,5"
how can with a cicle to extract only
50
-100
20,5
?
Hi,
Try this: /^-?\d+,?\d*$/.test(str), where str is a given string.
Returns true if str is a number in one of the formats you specified.
Regards, Mykola
http://marss.co.ua

Jun 12 '07 #2

P: n/a
ASM
artev a écrit :
if I have an array where the number are write in string format,
how can I recognize when a string is in realty a number?
example
if I have this
"50"
"house"
"light"
"100blue"
"yellow20"
"-100"
"20,5"
if(1*myArray[i] == myArray[i]) alert(myArray[i]+' is a number');
how can with a cicle to extract only
50
-100
20,5
var myArray = new Array(
"50",
"house",
"light",
"100blue",
"yellow20",
"-100",
"20,5"
);
var numbersArray = [];
for(var i=0; i<myArray.length; i++) {
var n = myArray[i].replace(',','.');
if(1*n == n) numbersArray.push(myArray[i]);
}
alert(numbersArray);

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Jun 17 '07 #3

P: n/a
On Jun 16, 7:13 pm, ASM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
if(1*myArray[i] == myArray[i]) alert(myArray[i]+' is a number');
or
if(!isNaN(myArray[i]) ////////

Jun 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.