这是重复的帖子,但要求略有不同,我想知道一个只允许十进制数字的正则表达式。在这里找到了类似的解决方案。
regular expression to allow only integer and decimal 但解决方案中有一个逗号。
我根本不需要正则表达式表达式中的命令。
/^[0-9]+([\,\.][0-9]+)?$/g; answer found on the site
我根据帖子中的解释应用了我的逻辑。
/^[0-9]+([\.][0-9]+)?$/g; My modification
还有其他方法可以解决这个问题吗?
最佳答案
这是正确的调用方式:
var regex = new RegExp(/^[0-9]*([\.][0-9]+)?$/g);
console.log(regex.test('0.85')); // true
console.log(regex.test('0,85')); // false
console.log(regex.test('.35')); // true
console.log(regex.test('')); // false
console.log(regex.test('.')); // false
console.log(regex.test('4')); // true
我建议对正则表达式进行一些修改,我已将第一个 block 的重数更改为零或更多
,因此.35
将被视为有效,如下所示你在上面的评论中问。
Here是个笨蛋。
关于javascript - 正则表达式仅允许整数和小数,不需要精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694000/