Forum Moderators: open

Message Too Old, No Replies

Get number from a string

         

charlee

10:00 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



is there an tandard function i can use to get a number out of a string such as "my value is 6" and just get the number 6?

birdbrain

10:51 pm on Jun 4, 2008 (gmt 0)



Hi there charlee,

does this help at all...


<script type="text/javascript">

var matches=[];
var strings=[
'Today is Saturday 24th',
'No. 120 Ridge Road',
'Sunday 16th December',
'This is Webmaster World',
'I am 43 years old',
'I moved house on the 30th',
'My salary is £20k per annum'
];

for(c=0;c<strings.length;c++) {

if(matches[c]=strings[c].match(/\d{1,}\.{0,}\d{0,}/)){;
alert('string '+(c+1)+ ', contains the number...\n\n '+matches[c]);
}
else {
alert('string '+(c+1)+ ', contains no numbers');
}
}

</script>


birdbrain

charlee

10:55 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



yup it does, thanks a lot. I barely remember how to create regular expressions but at least i understand them.

thanks :)

birdbrain

5:01 am on Jun 5, 2008 (gmt 0)



No problem, you're welcome. ;)