Extract a Number from a String using JavaScript (original) (raw)
Last Updated : 09 Jan, 2025
We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.
Below are the methods to extract a number from string using JavaScript:
Table of Content
- Using JavaScript match method with regEx
- Using JavaScript reduce() method with the spread operator
- Using JavaScript replace() method with regEx
- Using JavaScript for loop
- Using Array.prototype.filter with split
The number from a string in JavaScript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. The regular expression for extracting a number is (/(\d+)/).
**Example 1: This example uses the match() function to extract numbers from strings with regular expressions.
JavaScript `
// Function to extract numbers function myGeeks() {
// Input string
let str = "jhkj7682834";
console.log(str)
// Using match with regEx
let matches = str.match(/(\d+)/);
// Display output if number extracted
if (matches) {
console.log(matches[0]);
}
}
// Function call myGeeks();
`
Output
jhkj7682834 7682834
**Example 2: This example uses the match() function to extract numbers from strings.
JavaScript `
// Function to extract numbers function myGeeks() {
// Input string
let str = "foo35bar5jhkj88";
console.log(str)
// Using match with regEx
let matches = str.match(/\d+/g);
// Display output if number extracted
if (matches) {
console.log(matches[0] +', '+ matches[1]+', ' + matches[2]);
}
}
// Function call myGeeks();
`
Output
foo35bar5jhkj88 35, 5, 88
The Javascript **arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
**Example: This example spread operator to convert into an array and the reduce method to extract all the numbers from the string.
JavaScript `
// Function to extract numbers function myGeeks() { // Input string let str = "jhkj7682834"; let c = "0123456789"; console.log(str); function check(x) { return c.includes(x) ? true : false; } let matches = [...str].reduce( (x, y) => (check(y) ? x + y : x),"");
// Display output if number extracted
if (matches) {
console.log(matches);
}
}
// Function call myGeeks();
`
Output
jhkj7682834 7682834
The **string.replace() is an inbuilt method in JavaScript that is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.
**Example: In this example, we will use the JavaScript replace() method with regEx to extract the numbers out of given string
JavaScript `
// Function to extract numbers function myGeeks() { // Input string let str = "jhkj7682834"; console.log(str);
// Using match with regEx
let matches = str.replace(/[^0-9]/g, "");
// Display output if number extracted
if (matches) {
console.log(matches);
}
}
// Function call myGeeks();
`
Output
jhkj7682834 7682834
This approach loops through each character in the string and checks if it's a number using isNaN(). If the character is a number, it appends it to numberStr.
**Example: This example use for loop to extract all numbers present in the string.
JavaScript `
function extractNumbers(str) { let numbers = ""; for (let i = 0; i < str.length; i++) { if (!isNaN(str[i])) { numbers += str[i]; } } console.log(numbers) }
// Input string let str = "jhkj7682834hhdg237"; console.log(str); extractNumbers(str);
`
Output
jhkj7682834hhdg237 7682834237
Using Array.prototype.filter with split
Using Array.prototype.filter with split, you can extract numbers from a string by splitting the string into an array of characters, filtering out non-numeric characters, and joining the remaining characters back into a string.
**Example:
JavaScript `
let str = "The price is 123 dollars"; let number = str.split('').filter(char => !isNaN(char) && char !== ' ').join(''); console.log(number); // "123"
`
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.