• 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
  • VBA
  • About
You are here: Home / JavaScript / How to Remove Non Numbers From String in JavaScript

How to Remove Non Numbers From String in JavaScript

June 22, 2022 Leave a Comment

We can easily remove all non numbers from a string using JavaScript by making use of the JavaScript String replace() method along with the RegEx \D metacharacter.

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

someString = someString.replace(/\D/g, '');

console.log(someString);

#Output:
123455678887777790

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

Also notice that we make use of the RegEx \D metacharacter, which matches all non-digit characters.


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 all non numbers from a string variable.

The easiest way to get rid of non 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 non numbers, we use the \D metacharacter (“/\D/”) as the first argument and an empty string as the second argument.

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

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

someString = someString.replace(/\D/g, '');

console.log(someString);

#Output:
123455678887777790

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if Variable Exists
  • 2.  How to Select All Checkboxes in JavaScript
  • 3.  JavaScript indexOf – Get the position of a value within a string
  • 4.  JavaScript Capitalize First Letter of Every Word
  • 5.  Using JavaScript to Add Leading Zeros
  • 6.  How to Create a JavaScript Countdown Timer
  • 7.  JavaScript Change Background Color of Element
  • 8.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 9.  How to Repeat a String in JavaScript
  • 10.  Using JavaScript to Get Substring Between Two Characters

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy