Check If An Array of Strings Contains a Substring in JavaScript
Last Updated :
02 Jan, 2025
Here are the different ways to check if an array of strings contains a substring in JavaScript
1. Using includes() and filter() method
We will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const subArr = a.filter(str => str.includes(substr));
console.log(subArr);
Output[ 'Geeks', 'GeeksforGeeks' ]
In this example:
- The code filters the array to find all strings that contain the substring 'eks'.
- It uses the filter() method to create a new array (subArr) where each string from array is checked with includes().
- The includes() method returns true if the string contains 'eks'.
- The result is stored in subArr and printed.
2. Using includes() method and some() method
We will create an array of strings, and then use includes() and some() methods to check whether the array elements contain a sub-string.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const subArr = a.some(str => str.includes(substr));
console.log(subArr);
In this example:
- The code checks if any string in the array contains the substring 'eks'.
- It uses the some() method, which returns true if at least one string meets the condition (str.includes(substr)), otherwise false.
3. Using indexOf() method
Using indexOf() method that returns the index of the given value in the given collection.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc']
const substr = 'eks';
const subArr = a.indexOf(substr);
if (subArr) {
console.log("true")
} else {
console.log("false")
}
In this example:
- The code checks if any string in the array contains the substring 'eks'.
- It uses the some() method, which returns true if at least one string meets the condition (str.includes(substr)), otherwise false.
4. Using Lodash _.strContains() Method
Using the Lodash _.strContains() method that return the boolean value true if the given value is present in the given string else it returns false.
JavaScript
let _ = require('lodash-contrib');
let bool = _.strContains("abc", "a");
console.log("The String contains the "
+ "searched String : ", bool);
Output:
The String contains the searched String : false
In this example:
- The code uses Lodash's _.strContains() method from the lodash-contrib library to check if the string "abc" contains the substring "a".
- The result is stored in the variable bool.
5. Using a for of loop
Using a for of loop and indexOf iterates through each string in the array.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
let subArr = [];
for (let str of a) {
if (str.indexOf(substr) !== -1) {
subArr.push(str);
}
}
console.log(subArr);
Output[ 'Geeks', 'GeeksforGeeks' ]
In this example:
- The loop iterates over each string in arr.
- indexOf(substr) checks if 'eks' is in each string.
- Matching strings are pushed into subArr, which is then logged.
6. Using the reduce() Method
The reduce method used to accumulate all strings containing the substring into a new array.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const subArr = a.reduce((acc, str) => {
if (str.includes(substr)) {
acc.push(str);
}
return acc;
}, []);
console.log(subArr);
Output[ 'Geeks', 'GeeksforGeeks' ]
In this example:
- reduce() iterates through each string in the arr.
- For each string, it checks if 'eks' is included using includes().
- If it is, the string is pushed into the accumulator array (acc).
- The result is an array of matching strings, which is stored in subArr.
7. Using RegExp.test() Method
The RegExp.test() method is an effective way to check if a substring exists within any string in an array.
JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const regex = new RegExp(substr, 'i');
const subArr = a.filter(str => regex.test(str));
console.log(subArr);
Output[ 'Geeks', 'GeeksforGeeks' ]
In this example:
- new RegExp(substr, 'i') creates a regular expression for 'eks' with the 'i' flag to make the search case-insensitive.
- filter() checks each string in arr with regex.test(str) to see if it matches.
- The resulting array, subArr, contains the strings that include 'eks'
Similar Reads
Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin
3 min read
JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth
3 min read
Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
2 min read
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
How to check a given string is an anagram of another string in JavaScript ? In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram.An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different ter
3 min read
How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to find if an array contains a specific string in JavaScript/jQuery ? In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array
5 min read
Split a String into an Array of Words in JavaScript JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split method. This results in an array where each element represents a word from the original string. 1. Using split() MethodUsing split() method with a chos
5 min read
How to check a string data type is present in array using JavaScript ? In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu
3 min read
Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav
1 min read