You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求JavaScript匹配含逗号、小数点且最多两位小数的正数的正则表达式

适配JavaScript环境的目标正则表达式

直接给出可用的校验正则:
/^[1-9]\d{0,2}(,\d{3})*(\.\d{1,2})?$/

正则逻辑说明

  • 开头^[1-9]限定首位只能是1-9的数字,直接排除0、负数以及开头带多余0的非法格式
  • \d{0,2}匹配首位之后0到2位数字,适配1-3位的开头数值
  • (,\d{3})*匹配零个或多个千分位结构,保证每个逗号后面必须跟着3位数字,避免千分位格式错误
  • (\.\d{1,2})?匹配可选的小数部分,限定小数点后仅能有1到2位数字
  • 结尾$确保整个字符串完全匹配规则,不会出现中间夹杂非法字符的情况

JS环境使用示例

const numberCheckRegex = /^[1-9]\d{0,2}(,\d{3})*(\.\d{1,2})?$/;

// 合法用例验证
console.log(numberCheckRegex.test('1000')); // true
console.log(numberCheckRegex.test('1,000')); // true
console.log(numberCheckRegex.test('1,000.22')); // true

// 非法用例验证
console.log(numberCheckRegex.test('-2')); // false
console.log(numberCheckRegex.test('1,000.233')); // false
console.log(numberCheckRegex.test('0')); // false
console.log(numberCheckRegex.test('1.00,22')); // false

内容的提问来源于stack exchange,提问作者Loki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 03:51:01