We can remove all numbers from a string using JavaScript easily by making use of the JavaScript String replace() method.

var someString = "This12 is34 5a st5ri6n7g with numbe88877777rs90.";

someString = someString.replace(/[0123456789]/g, '');

console.log(someString);

#Output:
This is a string with numbers.

Notice in the replace method above, that instead of using .replace(/[0123456789]/, '') we use replace(/[0123456789]/g, ''). If we used the expression /[0123456789]/ in the replace function, it only replace the FIRST instance of a number. Using the regular expression /[0123456789]/g makes it so we replace ALL instances of a number in the string.

Note that we could have also written the code like this and it would produce the same result:

var someString = "This12 is34 5a st5ri6n7g with numbe88877777rs90.";

someString = someString.replace(/[0-9]/g, '');

console.log(someString);

#Output:
This is a string with numbers.

With the regular expression,.replace(/[0-9]/g, '') being the only difference.


When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables. One such manipulation is to remove numbers from a string variable.

The easiest way to get rid of numbers in a string using JavaScript is with the JavaScript String replace() function.

The replace() function takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove numbers, we pass the numbers (“/[0123456789]/”) as the first argument and an empty string as the second argument.

Below is our example again of how you can remove numbers from strings in JavaScript using the replace() function.

var someString = "This12 is34 5a st5ri6n7g with numbe88877777rs90.";

someString = someString.replace(/[0123456789]/g, '');

console.log(someString);

#Output:
This is a string with numbers.

Hopefully this article has been useful for you to learn how to remove all numbers from a string in JavaScript.

Categorized in:

JavaScript,

Last Update: March 8, 2024