nschamb1
nschamb1

Reputation: 19

Javascript Regex, finding numbers in a string

Sorry this is so specific but I couldn't figure it out from any of the examples I've seen or other questions. I need to find all positive or negative numbers with decimals of any precision in a string. examples: -0.2, .2, 3.4, -3.4, 3.400, 300.5 etc. They must have decimals, no digits and no commas. Any help would be greatly appreciated, I think it should be simple but I'm not getting anywhere.

Upvotes: 0

Views: 109

Answers (2)

oyss
oyss

Reputation: 692

try this one,I just test it in regex coach,fits your example:

"[^\d]\.\d+|[-|\d+|-\d+]\d*\.\d+|\.d+"

Upvotes: 0

Austin Greco
Austin Greco

Reputation: 33554

try this:

/\-?\d*\.\d+/

It will still work for -.2 not sure if you want that.

\-?        - or no -
\d*        0 or more digits
\.         .
\d+        1 or more digits

Also if you're sure the entire string will be the value with no other characters, then you should add ^ and $

/^\-?\d*\.\d+$/ would not match something like 'hello1.2' whereas without ^ and $ it would match.

Upvotes: 1

Related Questions