简体   繁体   English

删除任何非数字并检查格式是否为有效数字

[英]Remove Any Non-Digit And Check if Formatted as Valid Number

I'm trying to figure out a regex pattern that allows a string but removes anything that is not a digit, a . 我试图找出一个允许字符串的正则表达式模式,但删除任何不是数字的东西,a . , or a leading - . ,或领先-

I am looking for the simplest way of removing any non "number" variables from a string. 我正在寻找从字符串中删除任何非“数字”变量的最简单方法。 This solution doesn't have to be regex. 此解决方案不一定是正则表达式。

This means that it should turn 这意味着它应该转向

1.203.00 -> 1.20300
-1.203.00 -> -1.20300
-1.-1 -> -1.1
.1 -> .1
3.h3 -> 3.3
4h.34 -> 4.34
44 -> 44
4h -> 4

The rule would be that the first period is a decimal point, and every following one should be removed. 规则是第一个句点是小数点,后面的每一个都应该删除。 There should only be one minus sign in the string and it should be at the front. 字符串中应该只有一个减号,它应该在前面。

I was thinking there should be a regex for it, but I just can't wrap my head around it. 我当时认为应该有一个正则表达式,但我无法绕过它。 Most regex solutions I have figured out allow the second decimal point to remain in place. 我发现的大多数正则表达式解决方案允许第二个小数点保持不变。

You can use this replace approach: 您可以使用此替换方法:

  • In the first replace we are removing all non-digit and non-DOT characters. 在第一次replace我们删除所有非数字和非DOT字符。 Only exception is first hyphen that we negative using a lookahead. 唯一的例外是使用前瞻我们否定的第一个连字符。
  • In the second replace with a callback we are removing all the DOT after first DOT. 在第二次使用回调replace ,我们将在第一次DOT后删除所有DOT。

Code & Demo: 代码和演示:

 var nums = ['..1', '1..1', '1.203.00', '-1.203.00', '-1.-1', '.1', '3.h3', '4h.34', '4.34', '44', '4h' ] document.writeln("<pre>") for (i = 0; i < nums.length; i++) document.writeln(nums[i] + " => " + nums[i].replace(/(?!^-)[^\\d.]+/g, ""). replace(/^(-?\\d*\\.\\d*)([\\d.]+)$/, function($0, $1, $2) { return $1 + $2.replace(/[.]+/g, ''); })) document.writeln("</pre>") 

I can do it with a regex search-and-replace. 我可以通过正则表达式搜索和替换来实现。 num is the string passed in. num是传入的字符串。

num.replace(/[^\d\-\.]/g, '').replace(/(.)-/g, '$1').replace(/\.(\d*\.)*/, function(s) {
  return '.' + s.replace(/\./g, '');
});

A non-regex solution, implementing a trivial single-pass parser. 一种非正则表达式解决方案,实现了一个简单的单遍解析器。 Uses ES5 Array features because I like them, but will work just as well with a for-loop. 使用ES5 Array功能是因为我喜欢它们,但是对于for循环也能正常工作。

 function generousParse(input) { var sign = false, point = false; return input.split('').filter(function(char) { if (char.match(/[0-9]/)) { return sign = true; } else if (!sign && char === '-') { return sign = true; } else if (!point && char === '.') { return point = sign = true; } else { return false; } }).join(''); } var inputs = ['1.203.00', '-1.203.00', '-1.-1', '.1', '3.h3', '4h.34', '4.34', '4h.-34', '44', '4h', '.-1', '1..1']; console.log(inputs.map(generousParse)); 

Yes, it's longer than multiple regex replaces, but it's much easier to understand and see that it's correct. 是的,它比多个正则表达式替换更长,但它更容易理解并且看到它是正确的。

OK weak attempt but seems fine.. 好的弱尝试但看起来很好..

 var r = /^-?\\.?\\d+\\.?|(?=[az]).*|\\d+/g, str = "1.203.00\\n-1.203.00\\n-1.-1\\n.1\\n3.h3\\n4h.34\\n44\\n4h" sar = str.split("\\n").map(s=> s.match(r).join("").replace(/[az]/,"")); console.log(sar); 

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

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