How do I match and return a zero with a regular expression in javascript?

Posted by Hobhouse on Stack Overflow See other posts from Stack Overflow or by Hobhouse
Published on 2010-05-28T09:55:26Z Indexed on 2010/05/28 10:02 UTC
Read the original article Hit count: 276

Filed under:
|

I am trying to extract a number from a string. The number might be zero. Numbers appear like this: '+123', '-8' or '0'.

alert( '+123'.match(/[-?|+?]\d+/) );
alerts +123

alert( '-8'.match(/[-?|+?]\d+/) );
alerts -8

alert( '0'.match(/[-?|+?]\d+/) );
alerts null // why oh why?

How do I get '0'.match(/[-?|+?]\d+/) to return 0 instead of null?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex