Open In App

JavaScript Program to Extract Strings that contain Digit

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a Strings List, and the task is to extract those strings that contain at least one digit.

Example:

Input: test_list = [‘gf4g’, ‘is’, ‘best’, ‘gee1ks’] 
Output: [‘gf4g’, ‘gee1ks’]
Explanation: 4, and 1 are respective digits in a string.

Input: test_list = [‘gf4g’, ‘is’, ‘best’, ‘geeks’]
Output: [‘gf4g’]
Explanation: 4 is a digit in the string.

Using some() method extract strings that contain digit

This approach uses filter() to iterate over each string in the array. For each string, split('') is used to convert it into an array of characters. Then, some() is applied to the array of characters to check if at least one character can be converted to a number using parseInt() without resulting in NaN.

Example: This example shows the use of the above-explained approach.


Output
[ 'def456', 'hgt7ju', '789xyz' ]

Using regular expressions to extract strings that contain digit

In this approach the code checks for strings in an array that contains any digit. It uses the `filter` method to iterate through the array and a regular expression pattern, `/d`, to match any digit in each string. The result, an array of strings containing digits is stored in the variable `res` and then printed in the console.

Example: This example shows the use of the above-explained approach.


Output
The original list is : gf4g,is,best,4,gee1ks
Strings with any digit : gf4g,4,gee1ks

Using loop and replace() method to extract strings that contain digit

In this approach we are definig a function `fun` that checks if a string contains any digit using a loop and the `replace`method. The main script initializes an array, iterates through it, and collects strings containing digits using the`fun`function. The resulting array`res` is printed, that is containing strings with digits from the original list.

Example: This example shows the use of the above-explained approach.


Output
The original list is : gf4g,is,best,4,gee1ks
Strings with any digit : gf4g,4,gee1ks

Using Array.prototype.filter and a Loop

This approach filters an array of strings using Array.prototype.filter and a loop. It iterates through each string, checks each character, and returns true if any character is a digit (char >= '0' && char <= '9'), thereby extracting strings containing digits.

Example: In this example The function extractStringsContainingDigit filters an array to return strings containing at least one digit.


Output
The original List: hello,world1,test,abc123
String with any digit: world1,abc123

Using Array.prototype.reduce method

This approach utilizes the reduce method to iterate over each string in the array. For each string, it checks if it contains any digit using a regular expression (/\d/). If a string contains at least one digit, it is added to the result array. Finally, the function returns the result array containing strings with digits.


Output
Original List: [ 'gf4g', 'is', 'best', '4', 'gee1ks' ]
Strings with any digit: [ 'gf4g', '4', 'gee1ks' ]

Approach: Using Array.prototype.some with Regular Expression Check

In this approach, we utilize the Array.prototype.some method along with a regular expression check within the filter method to efficiently identify strings containing at least one digit. The some method will iterate through each character in the string and check if it matches the digit pattern using the regular expression.

Example: This example demonstrates the use of Array.prototype.some with a regular expression to extract strings that contain at least one digit.


Output
[ 'gf4g', 'gee1ks' ]
[ 'gf4g' ]



Next Article

Similar Reads