PDA

View Full Version : var anum=/(^\d+$)|(^\d+\.\d+$)/



zpzpzp
November 7th, 2008, 01:55 AM
Hi

I have read about this javascript code:

function isNumber(x){
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x)) return true else return false
}

It checks whether x is a number. however i do not know how var anum=/(^\d+$)|(^\d+\.\d+$)/ works. Could someone please explain?

Thanks a lot.

Paul

Emill
November 7th, 2008, 02:05 AM
That is called a regular expression. You can also do:
function isNumber(x){
if (/(^\d+$)|(^\d+\.\d+$)/.test(x)) return true else return false
}

supirman
November 7th, 2008, 02:16 AM
As the above poster said, it's a regular expression.

That's an easy one to decipher.

/(^\d+$)|(^\d+\.\d+$)/

The two in red are the two formats it's searching for.

To decipher it, you need to know some syntax rules (see this link (http://en.wikipedia.org/wiki/Regular_expression#Syntax)):

^ matches the beginning of the string
$ matches the end of the string
\d matches a digit
+ means match 1 or more of the preceeding
| means or

So, the first one will match any number of digits, ex:
3
64492923

The second piece matches 1 or more digits, a period, then one or more digits, ex:
23.53
24234234.22223


Hope that helps.