Welcome to WebmasterWorld Guest from 54.205.74.11

Forum Moderators: open

Selecting numbers from a string

Interger selection

   
4:53 am on Dec 14, 2007 (gmt 0)

5+ Year Member



I have a problem which I hope can be solved easily:

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

8:39 am on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Graham2107,

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

9:49 am on Dec 14, 2007 (gmt 0)

5+ Year Member



BirdBrain

Well done and THANK YOU VERY MUCH.

I tried the example and it work well. Now I just have to slot it in to the HMML code and see what comes out.

Did you work it out or was a case of using "standard" functions such as .match?

Graham
Durban
South Africa

2:20 pm on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



You should probably use /\d+/ as the regexp as this will match any number, not just 2 digits.
3:18 pm on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Graham2107,

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

1:14 pm on Dec 17, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Each and every string always contains a two digit integer number

So it does! Sorry I missed that, my bad!

2:07 pm on Dec 17, 2007 (gmt 0)

5+ Year Member



Thanks guys. I have used the d{2} and it works like a dream.

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

12:03 am on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



To find the offset I'd use indexof with the string you just matched. That will give you the start position of the digit.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month