简体   繁体   中英

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.

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.

试试这个,我只是在正则表达式教练中测试,适合您的示例:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM