How do I match and return a zero with a regular expression in javascript?
- by Hobhouse
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?