Javascript 正则表达式 - 替换非数字字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2555059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:53:35  来源:igfitidea点击:

Javascript Regular Expressions - Replace non-numeric characters

javascriptregex

提问by coffeemonitor

This works:

这有效:

var.replace(/[^0-9]+/g, '');  

That simple snippet will replace anything that is not a number with nothing.

这个简单的片段将用空替换任何不是数字的东西。

But decimals are real too. So, I'm trying to figure out how to include a period.

但小数也是实数。所以,我想弄清楚如何包含一个句点。

I'm sure it's really simple, but my tests aren't working.

我确信这真的很简单,但我的测试不起作用。

回答by Jacob Mattison

Did you escape the period? var.replace(/[^0-9\.]+/g, '');

你逃过了那个时期吗? var.replace(/[^0-9\.]+/g, '');

回答by Jeff B

Replacing something that is nota number is a little trickier than replacing something that isa number.

更换的东西,不是一个数量比更换的东西,有点麻烦,一个数字。

Those suggesting to simply add the dot, are ignoring the fact that . is also used as a period, so:

那些建议简单地添加点的人忽略了这样一个事实。也用作句点,因此:

This is a test. 0.9, 1, 2, 3will become .0.9123.

This is a test. 0.9, 1, 2, 3将成为.0.9123.

The specific regex in your problem will depend a lot on the purpose. If you only have a single number in your string, you could do this:

问题中的特定正则表达式在很大程度上取决于目的。如果你的字符串中只有一个数字,你可以这样做:

var.replace(/.*?(([0-9]*\.)?[0-9]+).*/g, "$1")

var.replace(/.*?(([0-9]*\.)?[0-9]+).*/g, "$1")

This finds the first number, and replaces the entire string with the matched number.

这将找到第一个数字,并用匹配的数字替换整个字符串。

回答by Levi Hackwith

Try this:

尝试这个:

var.replace(/[^0-9\.]+/g, '');

回答by Eric

Try this:

尝试这个:

var.replace(/[0-9]*\.?[0-9]+/g, '');

That only matches valid decimals (eg "1", "1.0", ".5", but not "1.0.22")

只匹配有效的小数(例如“1”、“1.0”、“.5”,而不是“1.0.22”)

回答by dkinzer

If you don't want to catch IP address along with decimals:

如果您不想捕获带有小数的 IP 地址:

var.replace(/[^0-9]+\.?[0-9]*/g, '');

Which will only catch numerals with one or zero periods

它只会捕获带有一个或零个句点的数字

回答by gm77

there's a lot of correct answers already, just pointing out that you might need to account for negative signs too.. "\-"add that to any existing answer to allow for negative numbers.

已经有很多正确答案,只是指出您可能也需要考虑负号……"\-"将其添加到任何现有答案中以允许负数。

回答by gordon

Here are a couple of jQuery input class types I use:

以下是我使用的几个 jQuery 输入类类型:

$("input.intgr").keyup(function (e) { // Filter non-digits from input value.
    if (/\D/g.test($(this).val())) $(this).val($(this).val().replace(/\D/g, ''));
});
$("input.nmbr").keyup(function (e) { // Filter non-numeric from input value.
    var tVal=$(this).val();
    if (tVal!="" && isNaN(tVal)){
        tVal=(tVal.substr(0,1).replace(/[^0-9\.\-]/, '')+tVal.substr(1).replace(/[^0-9\.]/, ''));
        var raVal=tVal.split(".")
        if(raVal.length>2)
            tVal=raVal[0]+"."+raVal.slice(1).join("");
        $(this).val(tVal);
    } 
});

intgrallows only numeric - like other solutions here.

intgr只允许数字 - 就像这里的其他解决方案一样。

nmbrallows only positive/negative decimal. Negative must be the first character (you can add "+" to the filter if you need it), strips -3.6.23.333to -3.623333

nmbr只允许正/负小数。负数必须是第一个字符(如果需要,可以在过滤器中添加“+”),条带-3.6.23.333-3.623333

I'm putting nmbrup because I got tired of trying to find the way to keep only 1 decimal and negative in 1st position

我把nmbr放在上面是因为我厌倦了试图找到在第一个位置只保留 1 个小数和负数的方法

回答by Mr. Adrien Feudjio

This one just worked for -veto +venumbers

这个只适用于-ve+ve数字

<input type="text" oninput="this.value = this.value.replace(/[^0-9\-]+/g, '').replace(/(\..*)\./g, '');">

回答by Eric

How about doing this:

这样做怎么样:

var numbers = str.gsub(/[0-9]*\.?[0-9]+/, "#{0} ");

回答by Pierre

Sweet and short inline replacing of non-numerical characters in the ASP.Net Textbox:

ASP.Net 文本框中非数字字符的甜蜜和短内联替换:

 <asp:TextBox ID="txtJobNo" runat="server" class="TextBoxStyle" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" />

Alter the regex part as you'ld like. Lots and lots of people complain about the cursor going straight to the end when using the arrow keys, but people tend to deal with this without noticing it for instance, arrow... arrow... arrow... okay then... backspace back space, enter the new chars.

根据需要更改正则表达式部分。很多人抱怨使用箭头键时光标会直接走到最后,但人们倾向于在没有注意到它的情况下处理这个问题,例如,箭头...箭头...箭头...好吧...退格退格,输入新字符。