JavaScript
String Manipulation
Programming
Web Development
Coding Tutorials

Strip all non-numeric characters from string in JavaScript

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In JavaScript, the fastest way to remove every non-digit character from a string is usually a regular expression replacement. The main thing to decide first is what “numeric” means in your case, because sometimes you want digits only, and other times you need to keep decimal points, signs, or locale-specific separators.

Digits Only

If you want only ASCII digits 0 through 9, use \D, which matches any character that is not a digit:

javascript
1function digitsOnly(value) {
2  return value.replace(/\D+/g, "");
3}
4
5console.log(digitsOnly("Phone: +1 (555) 123-4567"));
6console.log(digitsOnly("Order #A-9087-B"));

Output:

text
15551234567
9087

This is the right answer for tasks such as normalizing phone numbers, extracting ID digits, or removing punctuation from formatted numeric strings.

When You Need More Than Digits

Sometimes “strip non-numeric” does not actually mean digits only. For example:

  • money values may need one decimal point
  • signed values may need a leading minus sign
  • scientific notation may need e or E

In those cases, a blanket \D replacement is too aggressive.

A simple example that keeps digits, . and - looks like this:

javascript
1function numericLike(value) {
2  return value.replace(/[^0-9.-]+/g, "");
3}
4
5console.log(numericLike("Total: -12.50 USD"));

Output:

text
-12.50

This still does not fully validate the number format, but it shows how the allowed character set changes the regex.

Regex Is Usually Better Than Manual Loops

You could also loop through each character and keep only digits, but regular expressions are usually shorter and easier to scan.

A manual version looks like this:

javascript
1function digitsOnlyLoop(value) {
2  let result = "";
3  for (const ch of value) {
4    if (ch >= "0" && ch <= "9") {
5      result += ch;
6    }
7  }
8  return result;
9}
10
11console.log(digitsOnlyLoop("abc123def456"));

This works, but the intent is less direct than value.replace(/\D+/g, "").

Converting the Result to a Number

After stripping characters, you often want a number rather than a string. Use Number(...) or parseInt(...) depending on the exact behavior you want.

javascript
1const raw = "Invoice 0042";
2const digits = raw.replace(/\D+/g, "");
3const value = Number(digits);
4
5console.log(digits);
6console.log(value);

Be careful with empty results. If the source string contains no digits, digits becomes an empty string, and Number("") returns 0, which may not be what you want. In validation code, it is often better to check for an empty string first.

Unicode Digits and International Text

If your input can contain non-ASCII numerals, the classic \D pattern may not be enough for your requirements. JavaScript regular expressions also support Unicode property escapes in modern environments.

javascript
1function keepUnicodeDigits(value) {
2  return value.replace(/[^\p{Nd}]+/gu, "");
3}
4
5console.log(keepUnicodeDigits("Room 123"));

Use this only if you truly want Unicode decimal digits. For many web forms and APIs, ASCII digits are still the intended format.

Common Pitfalls

The most common mistake is using \D when the value is supposed to keep a decimal point or sign. That turns -12.5 into 125, which changes the meaning completely.

Another mistake is assuming that “numeric” automatically means “valid number.” A stripped string can still be malformed, such as --1..2, if your regex allows those characters without validating placement.

It is also easy to ignore empty inputs. If no digits exist, decide whether the result should be an empty string, null, or a validation error rather than blindly converting it.

Summary

  • For digits only, value.replace(/\D+/g, "") is usually the cleanest solution.
  • Decide whether you want digits only or a fuller numeric format that keeps signs or decimals.
  • Regex is usually simpler than manual character loops for this task.
  • Converting the cleaned string to a number requires separate validation logic.
  • Use Unicode digit handling only when your input format actually requires it.

Course illustration
Course illustration

All Rights Reserved.