JavaScript Program to Extract Strings that contain Digit
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.
Table of Content
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.
const strings = ["abc", "def456", "hgt7ju", "gfg", "789xyz"];
const result = strings.filter(str => str.split('').some(char => !isNaN(parseInt(char))));
console.log(result);
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.
// Initializing list
let test_list =
['gf4g', 'is', 'best', '4', 'gee1ks'];
// Printing original list
console.log("The original list is : " + test_list);
// Using regular expression to search for pattern \d
// which represents a digit in the string
let pattern = /\d/;
let res = test_list.filter((i) => pattern.test(i));
// Printing result
console.log("Strings with any digit : " + res);
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.
function fun(s) {
const digits = "0123456789";
let x = s;
for (let i of digits) {
s = s.replace(new RegExp(i, 'g'), "");
}
if (x.length !== s.length) {
return true;
}
return false;
}
// Initializing list
let test_list = ['gf4g', 'is', 'best', '4', 'gee1ks'];
// Printing original list
console.log("The original list is : " + test_list);
let res = [];
for (let i of test_list) {
if (fun(i)) {
res.push(i);
}
}
// Printing result
console.log("Strings with any digit : " + res);
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.
function extractStringsContainingDigit(arr) {
return arr.filter(str => {
for (const char of str) {
if (char >= '0' && char <= '9') {
return true;
}
}
return false;
});
}
let input = ["hello", "world1", "test", "abc123"];
console.log("The original List: " + input)
console.log("String with any digit: " + extractStringsContainingDigit(input));
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.
// Function to extract strings containing at least one digit
function extractStringsWithDigit(arr) {
return arr.reduce((result, str) => {
// Using regular expression to test if the string contains any digit
if (/\d/.test(str)) {
result.push(str);
}
return result;
}, []);
}
const test_list = ['gf4g', 'is', 'best', '4', 'gee1ks'];
console.log("Original List:", test_list);
console.log("Strings with any digit:", extractStringsWithDigit(test_list));
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.
function extractStringsContainingDigit(test_list) {
return test_list.filter(str => str.split('').some(char => /\d/.test(char)));
}
// Test cases
const testList1 = ['gf4g', 'is', 'best', 'gee1ks'];
const testList2 = ['gf4g', 'is', 'best', 'geeks'];
console.log(extractStringsContainingDigit(testList1)); // Output: ['gf4g', 'gee1ks']
console.log(extractStringsContainingDigit(testList2)); // Output: ['gf4g']
Output
[ 'gf4g', 'gee1ks' ] [ 'gf4g' ]