• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Remove All Numbers From String in JavaScript

How to Remove All Numbers From String in JavaScript

June 22, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Count Even Numbers in an Array
  • 2.  How to Count Vowels in a String Using JavaScript
  • 3.  Get the First Child Element and Change it with JavaScript
  • 4.  Convert String to Array in JavaScript
  • 5.  Sum the Digits of a Number in JavaScript
  • 6.  Using JavaScript to Remove Forward Slash From String
  • 7.  Set Hidden Field Value In JavaScript
  • 8.  Get the Size of a Set in JavaScript
  • 9.  JavaScript toFixed – How to Convert a Number to a String
  • 10.  How to Exit for Loop in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy