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:
Output:
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
eorE
In those cases, a blanket \D replacement is too aggressive.
A simple example that keeps digits, . and - looks like this:
Output:
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:
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.
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.
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.

