Remove all non-numeric characters from String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Table of Contents

  1. Remove all non-numeric characters from String in JavaScript
  2. Leaving dots in the result to preserve floating-point numbers
  3. Taking negative numbers into account
  4. Handling multiple dots in the string

# Remove all non-numeric characters from String in JavaScript

Use the String.replace() method to remove all non-numeric characters from a string.

The String.replace() method will remove all characters except the numbers in the string by replacing them with empty strings.

index.js
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, ''); console.log(result); // ๐Ÿ‘‰๏ธ 123456

remove all non numeric characters from string

The code for this article is available on GitHub

The first argument we passed to the String.replace() method is a regular expression.

The forward slashes / / mark the beginning and end of the regular expression.

index.js
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, '');
We used the g (global) flag to denote that the regular expression should match all occurrences in the string, and not just the first occurrence.

The \D character matches all non-digit characters.

The second argument we passed to the String.replace() method is the replacement for each match.

We used an empty string as the replacement to remove all non-numeric characters from the string.

index.js
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, ''); console.log(result); // ๐Ÿ‘‰๏ธ 123456
The replace() method doesn't change the original string, it returns a new string. Strings are immutable in JavaScript.

We replace all non-digit characters with an empty string to remove them from the string.

# Using a character class instead of the \D special character

You can also use a character class containing a range of digits to achieve the same result.

index.js
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/[^0-9]/g, ''); console.log(result); // ๐Ÿ‘‰๏ธ 123456

using character class instead of d special character

The code for this article is available on GitHub

The square brackets [] are called a character class and match the digits in the range.

When a caret ^ is used at the beginning of a character class, it means "Not the following".

In other words, match anything but the digits in the range from 0 to 9 and remove the matches.

This wouldn't work if you have floating-point numbers in your string because the dots . would also get stripped.

If you are trying to preserve a float number by keeping dots in the final result, things can go wrong if the string contains multiple dots or a dot in the wrong place.

# Leaving dots in the result to preserve floating-point numbers

If you want to leave dots in the result to try to preserve floating-point numbers, use the following regular expression.

index.js
const str = 'bobby 123 !@#$%^hadz?.456_com'; const result = str.replace(/[^\d.]/g, ''); console.log(result); // ๐Ÿ‘‰๏ธ 123.456

leaving dots in the result to preserve floating point numbers

The code for this article is available on GitHub

The forward slashes / / mark the beginning and end of the regular expression.

The part between the square brackets [] is called a character class and matches everything except for digits and dots.

The caret ^ symbol in the regex means "NOT the following".

We match all non-digits or dots in the example.

The \d character matches all digits in the range [0-9]. However, we prefixed the character with a caret ^ (not). So in its entirety, the regex matches all non-digit characters and dots.

As previously noted, things can go wrong if multiple dots exist in the string or a dot is at the wrong place.

# Taking negative numbers into account

If you need to take negative numbers into account, slightly tweak the regular expression.

index.js
const str = '-bobby 123 !@#$%^hadz?.456_com'; const result = str.replace(/[^\d.-]/g, ''); console.log(result); // ๐Ÿ‘‰๏ธ -123.456

taking negative numbers into account

The code for this article is available on GitHub

Notice that we added a minus sign - at the end of the character class.

# Handling multiple dots in the string

If you need to handle multiple dots in the string, use the parseFloat() function on the result.

index.js
const str = '-124.45.67 $'; const result = parseFloat(str.replace(/[^\d.-]/g, '')); console.log(result); // ๐Ÿ‘‰๏ธ -124.45

handling multiple dots in the string

We used the same regular expression to remove all non-numeric characters from the string.

The last step is to use the parseFloat() function to remove the part after the second period.

index.js
const str = '-124.45.67 $'; // ๐Ÿ‘‡๏ธ -124.45.67 console.log(str.replace(/[^\d.-]/g, '')); const result = parseFloat(str.replace(/[^\d.-]/g, '')); console.log(result); // ๐Ÿ‘‰๏ธ -124.45

The parseFloat() function takes a string as a parameter, parses the string and returns a floating-point number.

index.js
const str1 = '123.456abc'; console.log(parseFloat(str1)); // ๐Ÿ‘‰๏ธ 123.456 const str2 = '88.123bc'; console.log(parseFloat(str2)); // ๐Ÿ‘‰๏ธ 88.123
The code for this article is available on GitHub

As with parseInt(), the parseFloat() method ignores the non-numeric characters at the end of the string.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev