您需要如下所示的正则表达式才能正确执行此操作:/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/使用扩展修饰符(Perl支持)的带空格的相同表达式:/^ [+-]? ( (\d+ (\.\d*)?) | (\.\d+) ) $/x或带有注释:/^ # Beginning of string [+-]? # Optional plus or minus character ( # Followed by either: ( # Start of first option \d+ # One or more digits (\.\d*)? # Optionally followed by: one decimal point and zero or more digits ) # End of first option | # or (\.\d+) # One decimal point followed by one or more digits ) # End of grouping of the OR options $ # End of string (i.e. no extra characters remaining) /x # Extended modifier (allows whitespace & comments in regular expression)例如,它将匹配:12323.4534。.45-123-273.15-42。-.45+516+9.8+2。+.5并将拒绝这些非数字:。(单小数点)- (负小数点)+。(加上小数点)(空字符串)比较简单的解决方案可能会错误地拒绝有效数字或匹配这些非数字。