Pick out numbers from a string in JavaScript

In JavaScript, extracting numbers from strings is a common task that can be efficiently accomplished using regular expressions and built-in string methods. This approach allows you to find and extract numeric values from mixed content.

Understanding the Problem

The goal is to extract all numeric values from a given string. For example, from the string "I am 25 years old and earn $1500.50", we want to extract [25, 1500.50]. This involves pattern matching to identify numeric sequences including integers and decimal numbers.

Using Regular Expressions with match()

The most effective approach uses a regular expression pattern with the match() method to find all numeric occurrences in a string.

// Function to extract numbers from string
function extractNumbers(inputString) {
   // Regular expression to match integers and decimals
   const regex = /\d+(\.\d+)?/g;
   const matches = inputString.match(regex);
   
   // Convert string matches to numbers
   if (matches) {
      return matches.map(Number);
   }
   return []; // Return empty array if no numbers found
}

const inputString = "The price is $123.45 and quantity is 10 items";
const extractedNumbers = extractNumbers(inputString);
console.log(extractedNumbers);
[123.45, 10]

Handling Different Number Formats

You can modify the regular expression to handle various number formats including negative numbers and scientific notation.

function extractAllNumbers(inputString) {
   // Enhanced regex for negative numbers and scientific notation
   const regex = /-?\d+(\.\d+)?([eE][-+]?\d+)?/g;
   const matches = inputString.match(regex);
   
   return matches ? matches.map(Number) : [];
}

const complexString = "Temperature: -15.5°C, Distance: 2.5e3 meters";
const numbers = extractAllNumbers(complexString);
console.log(numbers);
[-15.5, 2500]

Alternative: Using replace() Method

Another approach uses replace() to collect numbers by replacing matches with a callback function.

function extractNumbersWithReplace(inputString) {
   const numbers = [];
   inputString.replace(/\d+(\.\d+)?/g, function(match) {
      numbers.push(Number(match));
      return match;
   });
   return numbers;
}

const testString = "Room 101 costs $250.75 per night";
const result = extractNumbersWithReplace(testString);
console.log(result);
[101, 250.75]

Comparison of Methods

Method Pros Cons
match() Simple, direct Returns null if no matches
replace() Never returns null Slightly more complex

Common Use Cases

This technique is useful for parsing user input, processing data files, extracting measurements from text, or cleaning numeric data from mixed content strings.

Conclusion

Regular expressions with match() provide the most straightforward way to extract numbers from strings in JavaScript. The pattern /\d+(\.\d+)?/g handles most common numeric formats effectively.

Updated on: 2026-03-15T23:19:00+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements