字符串是否为数字(包括小数)的正则表达式是什么?

What is the regular expression to the whether the string is Number(including decimals) or not?

让我们假设以下是我拥有的字符串。 1234 和 123.45 或 123.5687 我必须检查上面的东西是否是数字。它也应该 return 真正的小数。

myString.matches("\d+(?:\.\d+)?"); 将检查整数和小数。

^\d+(?:\.\d+)?$

您需要锚点来禁用部分匹配。

如果只有数字或只有一位小数且小数点后至少有一个数字的数字,这将 return 为真。它也适用于负数。 (^\-?\d*\.?\d+$)