Strip all non-numeric characters from string in JavaScript

JavascriptString

Javascript Problem Overview


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 - 9 should be kept.

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 Solutions


Solution 1 - Javascript

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

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

Solution 2 - Javascript

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

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

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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

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

Solution 5 - Javascript

Something along the lines of:

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

Solution 6 - Javascript

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));

Solution 7 - Javascript

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

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>

Solution 8 - Javascript

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.

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

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

Solution 9 - Javascript

try

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

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

Solution 10 - Javascript

This,

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

Handles both , and . also -

Example:

"Balance -$100,00.50".match(/\d|\.|\-/g).join('');

Outputs

10000.50

Solution 11 - Javascript

we are in 2017 now you can also use 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




Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionp.campbellView Question on Stackoverflow
Solution 1 - JavascriptcsjView Answer on Stackoverflow
Solution 2 - Javascriptmax4everView Answer on Stackoverflow
Solution 3 - JavascriptAuraseerView Answer on Stackoverflow
Solution 4 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 5 - JavascriptJan HančičView Answer on Stackoverflow
Solution 6 - JavascriptjackosaurView Answer on Stackoverflow
Solution 7 - JavascriptGrantView Answer on Stackoverflow
Solution 8 - JavascriptChaos LegionView Answer on Stackoverflow
Solution 9 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 10 - JavascriptjeffbRTCView Answer on Stackoverflow
Solution 11 - JavascriptFrank WisniewskiView Answer on Stackoverflow