How to Remove All Non-Numeric Characters from a String in JavaScript
A common data sanitization task is to strip all non-numeric characters from a string, leaving only the digits. This is useful for cleaning up user input, parsing formatted numbers, or extracting numerical data from a larger text block. The most efficient and powerful way to do this is with the String.prototype.replace() method using a simple regular expression.
This guide will teach you the standard method for removing all non-digits. We will also cover the more complex but important task of parsing a valid floating-point or integer number from a string that may contain currency symbols, commas, and other characters.
The Core Method: replace() with a Regular Expression
The replace() method, when used with a regular expression that has the global (g) flag, can find and replace all matches for a pattern in a string. To remove non-numeric characters, we simply use a regex that matches anything that is not a digit.
Problem: you have a string containing a mix of letters, symbols, and numbers, and you want to extract only the numbers.
// Problem: How to get just "123456" from this string?
const messyString = 'ID: 123-456-XYZ';
Solution: use the \D special character in a regular expression, which means "match any character that is not a digit."
const messyString = 'ID: 123-456-XYZ';
// The regex /\D/g matches all non-digit characters, globally.
// We replace them with an empty string ('').
const numbersOnly = messyString.replace(/\D/g, '');
console.log(numbersOnly); // Output: '123456'
This is the most concise and efficient way to strip all non-digit characters from a string.
How the Regular Expression Works
Let's break down the pattern /\D/g:
/ ... /: These forward slashes mark the beginning and end of the regular expression.\D: This is a special regex character class that matches any character that is not a digit (0-9). It is the inverse of\d, which matches any digit./g: This is the global flag. It is crucial. Without it,replace()would only find and remove the first non-digit character. The global flag ensures it replaces all occurrences throughout the string.
A More Advanced Task: Parsing a Formatted Number
The method above is perfect for getting a simple sequence of digits. However, a more common real-world task is to parse a formatted number string that might include a negative sign, decimal point, and thousands separators, like "-1,234.56".
The goal here is not just to keep digits, but to extract a single, valid number.
For example, here, simply using replace(/\D/g, '') would incorrectly produce '123456'.
// Problem: How to correctly parse this into the number -1234.56?
const formattedNumber = "SKU: -1,234.56 (USD)";
Solution: this requires a two-step process: first, remove all characters that are not a digit, a decimal point, or a negative sign. Then, parse the result with parseFloat().
/**
* Parses the first valid number from a formatted string.
* @param {string} str - The string to parse.
* @returns {number} The parsed number, or NaN if no valid number is found.
*/
function parseNumberFromString(str) {
// 1. Keep only digits, the first decimal point, and a potential leading minus sign.
// This regex finds the first valid number-like pattern.
const match = str.match(/-?\d+(\.\d+)?/);
if (!match) {
return NaN;
}
// 2. The first match is our number. Remove any thousands separators.
const numberString = match[0].replaceAll(',', '');
// 3. Parse the clean string.
return parseFloat(numberString);
}
// Example Usage:
const str1 = "SKU: -1,234.56 (USD)";
console.log(parseNumberFromString(str1)); // Output: -1234.56
const str2 = "Price: $5,678";
console.log(parseNumberFromString(str2)); // Output: 5678
const str3 = "No number here";
console.log(parseNumberFromString(str3)); // Output: NaN
Conclusion
Removing non-numeric characters from a string is a task where regular expressions excel.
- To strip all non-digit characters and get a string of only numbers, the recommended best practice is
string.replace(/\D/g, ''). - For the more complex task of parsing a formatted number (including decimals and negative signs) from a messy string, a more advanced, multi-step approach is required to first isolate the number-like pattern and then parse it.
By choosing the right regex for your specific goal, you can reliably sanitize and extract numerical data from any string.