Check If String Contains Substring From An Array In JavaScript

To check if String contains substring from an array in JavaScript, you can use any of the following methods –

  1. Use the Array.some() and str.includes() methods.
  2. Use the Array.filter() and str.includes() methods.
  3. Use the Array.some() and str.indexOf() methods.
  4. Use the Array.filter() and str.indexOf() methods.
  5. Use the Array.some() and str.match() methods.

Let’s discuss each of the above methods in detail below.

Check If String Contains Substring From An Array In JavaScript

check if string contains substring from an array

1. Use The Array.some() And str.includes() Methods

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value – true if at least one element passes the test, false otherwise.

The includes() method determines whether a string contains the characters of a specified string. It returns a Boolean value – true if the string contains the characters, false otherwise.

Now, to check if a string contains substring from an array, we can use the some() method along with the includes() method as shown below.

function checkSubstring(str, substrArr) {

return substrArr.some((substr) => str.includes(substr));

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // true

2. Use The Array.filter() And str.includes() Methods

The filter() method creates a new array with all the elements that pass the test implemented by the provided function. It returns an array containing only those elements that pass the test.

Now, to check if a string contains substring from an array, we can use the filter() method along with the includes() method as shown below.

function checkSubstring(str, substrArr) {

return substrArr.filter((substr) => str.includes(substr));

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // [‘I’, ‘love’]

3. Use The Array.some() And str.indexOf() Methods

The indexOf() method returns the position of the first occurrence of a specified value in a string. It returns -1 if the value is not found.

Now, to check if a string contains substring from an array, we can use the some() method along with the indexOf() method as shown below.

function checkSubstring(str, substrArr) {

return substrArr.some((substr) => str.indexOf(substr) !== -1);

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // true

Note that the indexOf() method is case sensitive. So, if you want to perform a case-insensitive search, you can use the toLowerCase() or toUpperCase() methods as shown below.

function checkSubstring(str, substrArr) {

return substrArr.some((substr) => str.toLowerCase().indexOf(substr.toLowerCase()) !== -1);

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // true

4. Use The Array.filter() And str.indexOf() Methods

To check if a string contains substring from an array, we can also use the filter() method along with the indexOf() method as shown below.

function checkSubstring(str, substrArr) {

return substrArr.filter((substr) => str.indexOf(substr) !== -1);

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // [‘I’, ‘love’]

This method is also case sensitive. So, if you want to perform a case-insensitive search, you can use the toLowerCase() or toUpperCase() methods as shown below.

function checkSubstring(str, substrArr) {

return substrArr.filter((substr) => str.toLowerCase().indexOf(substr.toLowerCase()) !== -1);

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // [‘I’, ‘love’]

5. Use The Array.some() And str.match() Methods

The match() method searches a string for a match against a regular expression, and returns an array containing the results of that search.

Now, to check if a string contains substring from an array, we can use the some() method along with the match() method as shown below.

function checkSubstring(str, substrArr) {

return substrArr.some((substr) => str.match(substr));

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // true

This method is also case sensitive. So, if you want to perform a case-insensitive search, you can use the toLowerCase() or toUpperCase() methods as shown below.

function checkSubstring(str, substrArr) {

return substrArr.some((substr) => str.toLowerCase().match(substr.toLowerCase()));

}

const result = checkSubstring("I love JavaScript", ["I", "love", "C++"]);

console.log(result); // true

Conclusion

In this article, we saw how to check if string contains substring from an array in JavaScript. There are multiple ways to do this and we saw some of them using the filter() method, the includes() method, the indexOf() method, and the match() method.

If you have any questions or suggestions, please feel free to leave a comment below.

Leave a Reply