Javascript: Remove all but numbers from string

This article will discuss removing all the non-numeric characters from a string in javascript using various methods and example illustrations.

Table of Contents:-

Remove all non-numeric characters from string in javascript

Using replace() and Regular Expression:-

The javascript’s replace() method will find a pattern within the string object and replaces it with a replacement. This pattern is passed in as the first parameter, and replacement is passed as the second parameter.

RegExp is the regular expression object. These objects are patterns used to match character combinations in strings.

Example:-

Remove all the non-numeric characters from the string “4509jk895772jh.bf#&ww#2111”

Code:-

let dummyString = "4509jk895772jh.bf#&ww#2111"
let digitsOnlyString = dummyString.replace(/\D/g,'')
console.log("STRING WITHOUT NON_NUMERIC CHARACTERS -> " + digitsOnlyString)

OR

const dummyString = "4509jk895772jh.bf#&ww#2111"
let digitsOnlyString = dummyString.replace(/[^0-9]/g,'');
console.log("STRING WITHOUT NON_NUMERIC CHARACTERS -> " + digitsOnlyString)

Explanation:-

The above code uses the replace() method, where regular expression /\D/g OR [^0-9] is passed as the first parameter. This regular expression states to find the pattern \D OR [^0-9] which means non -digit characters. Finally, the replace() method replaces them with a replacement (”) nothing in our case.

Output:-

STRING WITHOUT NON_NUMERIC CHARACTERS -> 45098957722111

Using match() and join():-

The match() method in javascript retrieves the result of the matching string against the regular expression passed in as an argument of the method.This method returns an array.

The join() method in javascript joins the elements of the array back into a string.

Example:-

Remove all the non-numeric characters from the string “4509jk895772jh.bf#&ww#2111”

Code:-

const dummyString = "4509jk895772jh.bf#&ww#2111"
let digitsOnlyString = dummyString.match(/\d/g).join('')
console.log("STRING WITHOUT NON_NUMERIC CHARACTERS ->" + digitsOnlyString)

Explanation:-

The above code uses the match() method to get an array of all the matching patterns in the string object (only the digit characters). The join() method finally joins them back into one string.

Output:-

STRING WITHOUT NON_NUMERIC CHARACTERS ->45098957722111

Remove all non-numeric characters from string in javascript except decimal

Using replace() and Regular Expression:-

Example:-

Remove all the non-numeric characters except the decimal from the string “4509jk895772jh.bf#&ww#2111”

Code:-

let dummyString = "4509jk895772jh.bf#&ww#2111"
let finalString = dummyString.replace(/[^0-9.]/g, '')
console.log("STRING WITHOUT NON_NUMERIC CHARACTERS BUT DECIMAL-> " + finalString)

Explanation:-

Here again, the above code uses the replace() method, where regular expression [^0-9.] is passed as the first parameter. This regular expression states to find the pattern of non-digit characters excluding decimal and replaces them with a replacement (”) nothing in our case. 

[^0-9.] here means anything except the ones in the bracket.

Output:-

STRING WITHOUT NON_NUMERIC CHARACTERS BUT DECIMAL-> 4509895772.2111

Read More:

I hope this article helped you delete all non-numeric characters with and without decimal from a string in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top