To remove all characters except numbers in JavaScript you can use the replace() function or for loop method. In this article we’ll introduce you the specific steps to do that. Let’s find out together!
Remove all characters except numbers in JavaScript
There are many ways to remove all characters except numbers, but I will give you the most straightforward methods.
In the example below, we need a variable to store the string of characters that we need to delete minus the number. Next, we will use the replace()
method to remove the characters except for numbers and save them in a new variable.
Using the replace() method
The replace() method finds the string for a specific value or a regex and returns a new string with the replaced values.
Syntax:
string.replace(searchValue, newValue)
Parameters:
searchValue
: is the value or expression which is replaced by the new value.newValue
: is the value which replaces the search value.
Code example 1: String is not containing spaces:
// Initialize a variable stores string var string1 = 'Learn2Share123.8<asdb341ba2/2>3IT'; // Return a new string with all numbers of digits var newString1 = string1.replace(/\D/g, ''); console.log("New string is:", newString1);
Output:
New string is: 21238341223
Code example 2: String contains spaces:
// Initialize a variable stores string var string2 = '23 @Learn 3 ^2Share 44ITadf % 2 / 3 23'; // Return a new string with all numbers of digits var newString2 = string2.replace(/\D/g,''); console.log ("New string is:", newString2);
Output:
New string is: 2332442323
Using the For Loop
Here, we will use for loop to delete. First, we create a variable res used to store numeric results. Second, we use the loop to run for the entire string length, take each character in the string to check if it is a number by the isNaN method, and then add the res variable, following the code example below.
Code example:
function removeStringExceptNumber(str) { // Initialize variable used for storage var res = ''; // Use for loop check if the number is added to the variable res for (let i = 0; i < str.length; i++) { if (!isNaN(str[i])) { res += str[i]; } } return res; } var string = "Learn2Share123.8<asdb341ba2/2>3I"; var newString = removeStringExceptNumber(string); console.log("New string is:", newString);
Output:
New string is: 21238341223
Summary
That’s all for today. We hope the ways to remove all characters except numbers in JavaScript that we mentioned above are helpful to you. If you have a question, don’t forget to leave a comment below. Have a nice day!
Maybe you are interested:
- Add Strings as Numbers in JavaScript
- Check if a Character is a Number using JavaScript
- Check if String is a Positive Integer using JavaScript
Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript