简体   繁体   English

Javascript 正则表达式正负十进制数

[英]Javascript regex positive negative decimal number

I need a regex that would match a positive and negative decimal value, empty string ('') and also minus only ('-').我需要一个正则表达式,它可以匹配正负十进制值、空字符串('')以及仅减号('-')。 Since I need it inside the function that handles the change of a numeric field and in order to add a negative value the user firstly adds the "-" or deletes the old value (""), I need to match all these 3 conditions.由于我需要在处理数字字段更改的 function 中使用它,并且为了添加负值,用户首先添加“-”或删除旧值(“”),我需要匹配所有这 3 个条件。

So, to sum up, the regex should allow:所以,总而言之,正则表达式应该允许:

  • positive & negative decimal values (also 0)正负十进制值(也是 0)
  • "" (empty string) ""(空字符串)
  • "-" (minus string) “-”(减号字符串)

What I have until now is:到目前为止,我所拥有的是:

function sanitizeInputValue(value: string) {
  var re = new RegExp('^$|^-?\\d{1,9}(\\.\\d{1,2})?$');
  return value.match(re) === null ? 0 : value;
}

Use javascript's built in Number to convert the string into a numeric value right before you return from the function.在从 function 返回之前,使用 javascript 的内置Number将字符串转换为数值。

Also, I modified your regex so that it can accept input like .23 and 0.2345 .另外,我修改了您的正则表达式,以便它可以接受像.230.2345这样的输入。

 function sanitizeInputValue(value) { var re = new RegExp('^$|^-?(\\d+)?(\\.?\\d*)?$'); return value.match(re) === null? 0: Number(value); } console.log(sanitizeInputValue("-23")); // -23 console.log(sanitizeInputValue("42.123")); // 42.123 console.log(sanitizeInputValue("-.34")); // -0.34 console.log(sanitizeInputValue("")); // 0 console.log(sanitizeInputValue("-8.")); // -8 console.log(sanitizeInputValue("-8.4")); // -8.4

You can make matching the digits with decimal parts optional so you would also match the hyphen only ^$|^-?(?:\d{1,9}(\.\d{1,2})?)?$您可以将带有小数部分的数字匹配为可选,因此您也可以仅匹配连字符^$|^-?(?:\d{1,9}(\.\d{1,2})?)?$

As then all parts are optional, you can omit the ^$ and then all parts are optional.由于所有部分都是可选的,因此您可以省略^$ ,然后所有部分都是可选的。

^-?(?:\d{1,9}(?:\.\d{1,2})?)?$

Explanation解释

  • ^ Start of string ^字符串开头
  • -? Optional hyphen可选连字符
  • (?: Non capture group (?:非捕获组
    • \d{1,9} match 1-9 times a digit \d{1,9}匹配一个数字 1-9 次
    • (?:\.\d{1,2})? Optionally match a dot and 1-2 digits可选择匹配一个点和 1-2 位数字
  • )? Close non capture group and make it optional as well关闭非捕获组并将其设为可选
  • $ End of string. $字符串结束。

Regex demo正则表达式演示

 var re = new RegExp("^-?(?:\\d{1,9}(?:\\.\\d{1,2})?)?$"); [ "", "-", "1", "1.2", "test", "1234567890", "1.123", "-2", "-2.6" ].forEach(s => console.log(s + " --> " + re.test(s)));

Edit编辑

As you also want to match -8.因为您还想匹配-8. and don't want to limit the amount of digits, you could use:并且不想限制位数,您可以使用:

^-?(?:\d+\.?\d*)?$

Explanation解释

  • ^ Start of string ^字符串开头
  • -? Optional -可选-
  • (?: Non capture group (?:非捕获组
    • \d+\.?\d* Match 1+ digits, optional dot and 0+ digits \d+\.?\d*匹配 1+ 个数字、可选的点和 0+ 个数字
  • )? Close group and make it optional关闭组并使其可选
  • $ End of string $字符串结尾

Regex demo正则表达式演示

 var re = new RegExp("^-?(?:\\d+\\.?\\d*)?$"); [ "-8.", "", "-", "1", "1.2", "test", "1234567890", "1.123", "-2", "-2.6" ].forEach(s => console.log(s + " --> " + re.test(s)) );

function sanitizeInputValue(value) {
  var re = /^$|\-?[0-9]\d*(\.\d*)?|-/;
  return value.match(re) ? value : 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM