在 JavaScript 中从字符串中去除所有非数字字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1862130/
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-22 21:40:13  来源:igfitidea点击:

Strip all non-numeric characters from string in JavaScript

javascriptstring

提问by p.campbell

Consider a non-DOM scenario where you'd want to remove all non-numeric characters from a string using JavaScript/ECMAScript. Any characters that are in range 0 - 9should be kept.

考虑一个非 DOM 场景,您希望使用 JavaScript/ECMAScript 从字符串中删除所有非数字字符。0 - 9应保留范围内的任何字符。

var myString = 'abc123.8<blah>';

//desired output is 1238

How would you achieve this in plain JavaScript? Please remember this is a non-DOM scenario, so jQuery and other solutions involving browser and keypress events aren't suitable.

你将如何在纯 JavaScript 中实现这一点?请记住,这是一个非 DOM 场景,因此 jQuery 和其他涉及浏览器和按键事件的解决方案不适合。

回答by csj

Use the string's .replacemethod with a regex of \D, which is a shorthand character class that matches all non-digits:

使用.replace带有正则表达式的字符串方法\D,它是匹配所有非数字的速记字符类:

myString = myString.replace(/\D/g,'');

回答by max4ever

If you need this to leave the dot for float numbers, use this

如果您需要它为浮点数留下点,请使用此

var s = "-12345.50 ".replace(/[^\d.-]/g, ''); // gives "-12345.50"

回答by Auraseer

Use a regular expression, if your script implementation supports them. Something like:

如果您的脚本实现支持正则表达式,请使用正则表达式。就像是:

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

回答by CMS

You can use a RegExp to replaceall the non-digit characters:

您可以使用 RegExp替换所有非数字字符:

var myString = 'abc123.8<blah>';
myString = myString.replace(/[^\d]/g, ''); // 1238

回答by Jan Han?i?

Something along the lines of:

类似的东西:

yourString = yourString.replace ( /[^0-9]/g, '' );

回答by Kamil Kie?czewski

try

尝试

myString.match(/\d/g).join``

var myString = 'abc123.8<blah>'
console.log( myString.match(/\d/g).join`` );

回答by Grant

In Angular / Ionic / VueJS -- I just came up with a simple method of:

在 Angular / Ionic / VueJS 中——我只是想出了一个简单的方法:

stripNaN(txt: any) {
    return txt.toString().replace(/[^a-zA-Z0-9]/g, "");
}

Usage on the view:

视图上的用法:

<a [href]="'tel:'+stripNaN(single.meta['phone'])" [innerHTML]="stripNaN(single.meta['phone'])"></a>

回答by Chaos Legion

Unfortunately none of the answers above worked for me.

不幸的是,上面的答案都不适合我。

I was looking to convert currency numbers from strings like $123,232,122.11(1232332122.11) or USD 123,122.892(123122.892) or any currency like ? 98,79,112.50(9879112.5) to give me a number output including the decimal pointer.

我希望将货币数字从$123,232,122.11(1232332122.11) 或USD 123,122.892(123122.892) 或任何货币? 98,79,112.50(9879112.5)等字符串转换为我的数字输出,包括小数点指针。

Had to make my own regex which looks something like this:

必须制作我自己的正则表达式,它看起来像这样:

str = str.match(/\d|\./g).join('');

回答by Hymanosaur

Short function to remove all non-numeric characters but keep the decimal (and return the number):

删除所有非数字字符但保留小数(并返回数字)的短函数:

parseNum = str => +str.replace(/[^.\d]/g, '');
let str = 'a1b2c.d3e';
console.log(parseNum(str));

回答by Frank Wisniewski

we are in 2017now you can also use ES2016

我们在2017现在你也可以使用ES2016

var a = 'abc123.8<blah>';
console.log([...a].filter( e => isFinite(e)).join(''));

or

或者

console.log([...'abc123.8<blah>'].filter( e => isFinite(e)).join(''));  

The result is

结果是

1238