【问题标题】:Javascript Regular expression for basic digits with decimal points?带有小数点的基本数字的Javascript正则表达式?
【发布时间】:2018-03-24 00:15:25
【问题描述】:

我正在尝试使用正则表达式来验证十进制值。我在正则表达式下面写了,但它不允许第一个小数的值像 .5 或 .6 或 .1

常规经验/^\d[0-9]{0,13}(\.\d{1,2})?$/

规则:

  1. 它应该允许正数。
  2. 小数点前最多允许 13 个数字
  3. 它应该允许在小数点后最多两个数字。
  4. 它应该允许 .(点)的数字像 .5
  5. 它不应该允许 .0

示例 - 有效输入

  • 0
  • 0.5
  • 1.55
  • .5
  • 1234567890123(小数点前13位)
  • 1234567890123.5
  • 1234567890123.00

示例 - 无效输入

  • .(点),
  • .0
  • 1.234
  • 5.
  • 12345678901234(小数点前14位)
  • 12345678901234.56

const valid = [
  "0",
  "0.5",
  "1.55",
  ".5",
  "1234567890123",
  "1234567890123.5",
  "1234567890123.00",
];

const invalid = [
  ".",
  ".0",
  "1.234",
  "5.",
  "12345678901234",
  "12345678901234.56",
];

const rgx = /^\d[0-9]{0,13}(\.\d{1,2})?$/

console.log("Checking valid strings (should all be true):");
valid.forEach(str => console.log(rgx.test(str), str));

console.log("\nChecking invalid strings (should all be false):");
invalid.forEach(str => console.log(rgx.test(str), str));

【问题讨论】:

  • 在你的正则表达式中从 ~ \d[0-9]{0,13} ~ 中删除 [0-9],然后它将允许 .5 或 .6 或 .1 等
  • 您的问题实际上太宽泛了,因为缺少许多其他边缘情况。

标签: javascript regex


【解决方案1】:

我相信以下正则表达式应该满足您的所有条件:

^(\d{1,13}($|\.\d?\d$)|\.[1-9]\d?$)

第一种情况:1-13 位数字后跟无或后跟“。”后跟一位或两位数

第二种情况:一个“.”后跟一个非零数字,最多一个其他数字

const valid = [
  "0",
  "0.5",
  "1.55",
  ".5",
  "1234567890123",
  "1234567890123.5",
  "1234567890123.00",
];

const invalid = [
  ".",
  ".0",
  "1.234",
  "5.",
  "12345678901234",
  "12345678901234.56",
];

const rgx = /^(\d{1,13}($|\.\d?\d$)|\.[1-9]\d?$)/

console.log("Checking valid strings (should all be true):");
valid.forEach(str => console.log(rgx.test(str), str));

console.log("\nChecking invalid strings (should all be false):");
invalid.forEach(str => console.log(rgx.test(str), str));

【讨论】:

    【解决方案2】:

    我认为这应该满足您的大部分要求,但不是全部,限制为小数点后 9 位

    ( /^(\d+\.?\d{0,9}|\.\d{1,9})$/ )
    

    这个没有小数限制

    ( /^(\d+\.?\d*|\.\d+)$/ )
    

    【讨论】:

    • 是的,我想不出比分别处理xxx.xx.xx 情况更好的解决方案
    • 我也是,我希望我的意见能帮助你解决这个问题
    • 我发现这个案例与5.输入不正确
    • 我觉得应该是/^(\d+\.?\d{1,9}|\.\d{1,9})$/
    【解决方案3】:

    其他答案不允许 .0x 的情况,我认为这是有效的?我认为您需要分别测试 xxx[.xx] 和 .xx 即

    ^(\d{1,13}(\.\d{1,2})?|\.(0[1-9]|[1-9]\d?))$
    

    【讨论】:

      【解决方案4】:

      要匹配您的值,您可以使用non capturing groupalternation,使用| 并指定您要匹配的内容。

      ^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$

      这也将匹配 .01 而不是 .0

      说明

      • ^ 字符串开头
      • (?:非捕获组
        • \.[1-9][0-9]? 匹配点后面不跟 0 和 0-9
        • |或者
        • \d{1,13} 匹配 1 - 13 位数字
        • (?:非捕获组
          • \.\d{1,2} 匹配一个点和 1 或 2 位数字
        • )?关闭非捕获组并使其可选
        • |或者
        • .\d{2} 匹配点后跟 2 位数字
      • )关闭非捕获组
      • $ 字符串结束

      const valid = [
        "0",
        ".01",
        "0.5",
        "1.55",
        ".5",
        "1234567890123",
        "1234567890123.5",
        "1234567890123.00",
      ];
      
      const invalid = [
        ".",
        ".0",
        "1.234",
        "5.",
        "12345678901234",
        "12345678901234.56",
      ];
      
      const rgx = /^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$/;
      
      console.log("Checking valid strings (should all be true):");
      valid.forEach(str => console.log(rgx.test(str), str));
      
      console.log("\nChecking invalid strings (should all be false):");
      invalid.forEach(str => console.log(rgx.test(str), str));

      不匹配 .01 你可以使用:

      ^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$

      const valid = [
        "0",
        "0.5",
        "1.55",
        ".5",
        "1234567890123",
        "1234567890123.5",
        "1234567890123.00",
      ];
      
      const invalid = [
        ".",
        ".0",
        ".01",
        "1.234",
        "5.",
        "12345678901234",
        "12345678901234.56",
      ];
      
      const rgx = /^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$/;
      
      console.log("Checking valid strings (should all be true):");
      valid.forEach(str => console.log(rgx.test(str), str));
      
      console.log("\nChecking invalid strings (should all be false):");
      invalid.forEach(str => console.log(rgx.test(str), str));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多