Regex to match 2 digits, optional decimal, two digits

后端 未结 11 1362
时光取名叫无心
时光取名叫无心 2020-11-30 09:29

I\'ve spent half an hour trying to get this, maybe someone can come up with it quickly.

I need a regular expression that will match one or two digits, followed by an

11条回答
  •  情歌与酒
    2020-11-30 10:23

    you can use

    let regex = new RegExp( ^(?=[0-9.]{1,${maxlength}}$)[0-9]+(?:\.[0-9]{0,${decimal_number}})?$ );

    where you can decide the maximum length and up to which decimal point you want to control the value

    Use ES6 String interpolation to wrap the variables ${maxlength} and ${decimal_number}.

提交回复
热议问题