John Mwaniki /   27 Dec 2023

How to Remove Numbers from Strings in JavaScript

When dealing with strings in JavaScript, you may at some point want to remove numbers from a string, leaving it composed solely of non-numeric characters.

This can be applicable in various instances such as data cleaning, text formatting, and user input validation.

In this article, you will learn various methods and techniques to remove numbers from strings in JavaScript with the help of examples.

1. Using Regular Expressions

Regular expressions (regex) offer an efficient way to remove numbers from strings. Using the replace() function, combined with a regex pattern, you can easily achieve this.

The first argument in the replace() function specifies the text to replace while the second argument specifies what to replace it with. If we pass a regular expression pattern as the first argument, everything that matches it gets replaced by the second argument's value.

Example

var str = "Hello World2024!";
var newStr = str.replace(/\d/g, '');

console.log(newStr);

Output:
Hello World!

In this example, we have passed the first argument of the replace() function as a regex pattern that matches digits (0-9) and the second as an empty string '' as their replacement value, effectively removing all the occurrences of digits.

2. Iteration and Number Detection

You can use a loop to iterate through each character of the string and construct a new string excluding numeric characters.

Example 1

var str = "Abc3d0e8Fgh1ij57kL";
var resultString = "";

for (var i = 0; i <= str.length - 1; i++) {
    if (isNaN(str[i])) {
        resultString += str[i];
    }
}

console.log(resultString);

Output:
AbcdeFghijkL

In this example, we first created an empty string (resultString). We then iterated through each character in the original string through the for() loop, checking if it is a number or not using the isNaN() function which returns true for non-numeric values. If the character is not a number, we concatenate it to the new string.

By the end of the iteration, we effectively eliminated all numeric characters.

Example 2

var str = "abc123xyz456";
var resultString = "";

for (let char of str) {
    if (isNaN(char)) {
        resultString += char;
    }
}

console.log(resultString);

Output:
abcxyz

3. Splitting and Filtering

We can also use the split(), filter(), and join() array methods to achieve the desired result.

Example

var str = "Hello123World456";
var resultString = str.split("")
    .filter(char => isNaN(char))
    .join("");

console.log(resultString);

Output:
HelloWorld

In this example, we have used the split("") method to create an array of characters, filter() method together with isNaN() to retain only non-numeric characters, and join("") to combine the filtered characters back into a string.

Conclusion

Removing numbers from strings in JavaScript can be accomplished through various methods. You can use regular expressions, iterate through characters, or use array methods to suit different scenarios. Choose the method that aligns with your specific requirements and coding style.