Pick out numbers from a string in JavaScript


In the given problem we are required to find out the numbers from a given string with the help of Javascript functionalities. So we will use regular expressions and some predefined functions of Javascript.

Understanding the Problem

The problem at hand is to find the numbers from the given string in Javascript. This task can be useful sometimes. To accomplish this task we will use regular expressions and also some string manipulation functions of Javascript. So in simple terms we will be given a string in this string there will be some integer or other type of number that can be present. So our task is to find out these numbers from the string and show them in the separate array as a result. For example suppose we have a string as "I am 25 years old" so in this string there is a number present 25. So we will be extracting that number from the string.

Logic for the given Problem

To solve the given problem to extract numbers from a given string we will use searching technique basically. So the logic will involve searching for numeric patterns in the given string. For searching we will be using the regular expressions which is a powerful tool whenever we have to match any pattern. It allows us to find the exact pattern and extract the numbers.

Algorithm

Step 1: As our task is to find the numbers from the given string. For solving this we will define a function called extractNumbers and in this function we will take a parameter as inputString. So this inputString will contain numbers.

Step 2: Now we have defined a function, so inside the function we will define a regular expression pattern which is a main property of our algorithm. Because we have to match numeric values.

Step 3: After that we will use the predefined method of Javascript called match. In this method the regular expression pattern to get all the occurrences of numbers from the given string. So basically this method will return an array of matches.

Step 4: After having the matches our next task is to convert the matched values from strings to the numbers if this is needed.

Step 5: And lastly we will return the array of extracted numbers.

Example

// Function to extract number from the string
function extractNumbers(inputString) {
   const regex = /(\d+(\.\d+)?)/g;
   const matches = inputString.match(regex);
   const numbers = matches.map(Number);
   return numbers;
}

const inputString = "The string contains numbers 123 and 45.6";
const extractedNumbers = extractNumbers(inputString);
console.log(extractedNumbers);

Output

[123, 45.6]

Complexity

Time complexity of the code above depends on the size of the input string and the number of numbers there are in the given string. So let's say n shows the size of the input string. The time complexity will be considered as O(n). As we traverse the string one time to find all the matches. And the space complexity is O(m), in which m is the number of numbers found in the string.

Conclusion

So the solution was straightforward for the given problem of finding the numbers from the given string. As we have used regular expressions and some string manipulation techniques of Javascript. With the help of regular expressions we are able to match the numeric values and we were able to pick out the numbers successfully.

Updated on: 16-Aug-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements