Forum Moderators: open
1) I have a string in a form of variable length - max 30 chars
2) Each and every string always contains a two digit integer number
3) The actual integer number varies from one string to another, and the value is unknown
4) The position of the integer number varies within the string
I need to search the string for the integer and then use it as a variable for other calculations.
String examples:
1) Today is Saturday 24th
2) No. 12 Ridge Road
3) Sunday 16th December
4) I am 43 years old
5) I moved house on the 30th
Is there a Javascript command(s) that I can use to search the string, not for a match as I don't know what I am looking for, but for an integer number in that string and create a variable from it?
Graham
Durban
South Africa
and a warm welcome to these forums. ;)
try it like this...
<script type="text/javascript">
var matches=[];
var strings=
[
'Today is Saturday 24th',
'No. 12 Ridge Road',
'Sunday 16th December',
'I am 43 years old',
'I moved house on the 30th'
];
for(c=0;c<strings.length;c++) {
matches[c]=strings[c].match(/\d{2}/,strings[c]);
alert('string '+c+ ' containtains the number '+matches[c]);
}
</script>
birdbrain
Did you work it out or was a case of using "standard" functions such as .match?
Yes that is correct and, of course, a basic RegExpression. ;)
Hi there Dabrowski,
You should probably use /\d+/ as the regexp as this will match any number, not just 2 digits.
That is true, but I did not use it as the OP specifically said ...
Each and every string always contains a two digit integer number
birdbrain
I now not only find the integer but find where it starts in the string and slice the first part of for another search.
Great help and thanks. This forum is fantastic for questions like this when you have searched the web for javascript pages for the answer without finding anything.
Graham
Durban
South Africa