【问题标题】:RegExp to match fraction and decimal [duplicate]正则表达式匹配分数和小数[重复]
【发布时间】:2021-07-20 15:31:46
【问题描述】:

我正在尝试执行正则表达式来匹配分数和小数。 用户将输入小数或分数,例如 22、11、1/4、15 1/2、2.5、55.3 等。

我试过这样,但它不允许我 15 1/4

我的正则表达式..

^[0-9]*([.|/][1-9]+)?$

【问题讨论】:

    标签: javascript regex regex-group


    【解决方案1】:

    const regex = /\d+([\s\.\/]\d)*/g;
    const str = `22, 11, 1/4, 15 1/2, 2.5, 55.3`;
    
    const result = str.match(regex);
    console.log( result );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多