Open In App

JavaScript Program to Extract Strings that contain Digit

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

Javascript
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.

Javascript
// 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.

Javascript
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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads