homepage Welcome to WebmasterWorld Guest from 54.163.84.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Selecting numbers from a string
Interger selection
Graham2107




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

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

 

birdbrain




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

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

Graham2107




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

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

Dabrowski




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

You should probably use /\d+/ as the regexp as this will match any number, not just 2 digits.

birdbrain




msg:3527744
 3:18 pm on Dec 14, 2007 (gmt 0)

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

Dabrowski




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

Each and every string always contains a two digit integer number

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

Graham2107




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

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

Dabrowski




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved