Skip to main content

How to Remove or Replace All Numbers in a String in JavaScript

A common text sanitization task is to remove all numeric characters from a string or replace them with another character. For example, you might want to remove version numbers from a title or replace all digits with a placeholder. The most powerful and efficient way to do this is with the String.prototype.replace() method using a regular expression.

This guide will teach you how to use a simple regular expression to replace or remove all numbers from a string, and how to adapt the pattern to handle consecutive blocks of numbers as a single unit.

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 target numbers, we use a regex that matches digit characters.

Problem: you have a string containing a mix of letters and numbers, and you want to remove or replace all the digits.

// Problem: How to remove or replace all the numbers in this string?
let text = 'User ID: 123, Role: 45';

Replacing Each Individual Digit

If your goal is to replace every single digit character one by one, you can use the \d special character in your regex.

Solution: the regex /\d/g will find every individual digit in the string.

let text = 'User ID: 123, Role: 45';

// Remove all numbers
let withoutNumbers = text.replace(/\d/g, '');
console.log(withoutNumbers); // Output: 'User ID: , Role: '

// Replace all numbers with a placeholder '#'
let withPlaceholders = text.replace(/\d/g, '#');
console.log(withPlaceholders); // Output: 'User ID: ###, Role: ##'

Output:

User ID: , Role: 
User ID: ###, Role: ##

How It Works

  • / ... /g: The forward slashes denote a regular expression, and the g is the global flag, which is essential for replacing all occurrences, not just the first.
  • \d: This is a special regex character class that matches any single digit (0-9). It's a convenient shortcut for [0-9].

Replacing Each Block of Consecutive Digits

A more common requirement is to treat a sequence of digits (like "123") as a single unit and replace the entire block with just one character.

Problem: you want to replace "123" with a single placeholder, not three separate ones.

let text = 'User ID: 123, Role: 45';

Solution: to achieve this, you add a quantifier (+) to your regular expression. The regex /\d+/g will find every block of one or more consecutive digits.

let text = 'User ID: 123, Role: 45';

// The '+' means "one or more of the preceding character".
let replacedBlocks = text.replace(/\d+/g, 'X');

console.log(replacedBlocks);

Output:

User ID: X, Role: X
note

This is often the more useful approach for sanitizing data, as it treats each number as a whole.

Conclusion

Replacing numbers in a string is a simple and efficient task when using regular expressions.

  • To replace every individual digit, the recommended method is string.replace(/\d/g, '...').
  • To replace each block of one or more consecutive digits with a single replacement, add a + quantifier: string.replace(/\d+/g, '...').
  • To remove the numbers, simply provide an empty string ('') as the replacement value.

By choosing the right regex pattern, you can precisely control how numeric characters are handled in your string manipulations.