How to Search for Multiple Words Within a String or Array in JavaScript?

Last Updated : 14 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To search for multiple words within a string or an array in JavaScript, you can use different approaches depending on the data structure. Here’s how to perform the search effectively:

1. Searching for Multiple Words in a String

To check if a string contains multiple words, you can use String.prototype.includes(), regular expressions, or other string manipulation methods.

Example 1: Using Regular Expressions

JavaScript
const text = "The quick brown fox jumps over the lazy dog";
const wordsToSearch = ["quick", "fox", "dog"];

const regex = new RegExp(wordsToSearch.join("|"), "gi");
const matches = text.match(regex);

if (matches) {
  console.log("Found words:", matches); // Output: ["quick", "fox", "dog"]
} else {
  console.log("No matches found");
}

Output
Found words: [ 'quick', 'fox', 'dog' ]

Explanation:

  • wordsToSearch.join("|") creates a regular expression pattern that matches any of the words.
  • RegExp with the gi flags makes the search case-insensitive and global (searching the entire string).

Example 2: Using includes()

JavaScript
const array = ["apple", "banana", "cherry", "date", "elderberry"];
const wordsToSearch = ["banana", "date", "fig"];

const foundItems = array.filter(item => wordsToSearch.includes(item));
console.log("Matching words in array:", foundItems); // Output: ["banana", "date"]

Output
Matching words in array: [ 'banana', 'date' ]

Explanation:

  • Array.prototype.filter() creates an array of words that are found in the string using includes().

2. Searching for Multiple Words in an Array

To search for multiple words within an array, you can use methods like Array.prototype.filter(), some(), or a combination of forEach() and other conditional logic.

Example: Using filter() and some()

JavaScript
const array = ["apple", "banana", "cherry", "date", "elderberry"];
const wordsToSearch = ["banana", "date", "fig"];

const foundItems = array.filter(item => wordsToSearch.includes(item));
console.log("Matching words in array:", foundItems); // Output: ["banana", "date"]

Output
Matching words in array: [ 'banana', 'date' ]

Explanation:

  • filter() iterates through each element in the array and keeps elements that match any of the words in wordsToSearch.

Example: Case-Insensitive Search Using Regular Expressions

JavaScript
const array = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
const wordsToSearch = ["banana", "date", "fig"];

const regex = new RegExp(wordsToSearch.join("|"), "i");
const foundItems = array.filter(item => regex.test(item));
console.log("Matching words (case-insensitive):", foundItems); // Output: ["Banana", "Date"]

Output
Matching words (case-insensitive): [ 'Banana', 'Date' ]

3. Combining String Search and Array Search

If you have a complex scenario where you need to search for multiple words in both a string and an array, you can combine these approaches:

JavaScript
const text = "The quick brown fox jumps over the lazy dog";
const array = ["apple", "banana", "fox", "dog"];
const wordsToSearch = ["quick", "fox", "banana"];

const textMatches = wordsToSearch.filter(word => text.includes(word));
const arrayMatches = array.filter(item => wordsToSearch.includes(item));

console.log("Matches in text:", textMatches); // Output: ["quick", "fox"]
console.log("Matches in array:", arrayMatches); // Output: ["fox", "banana"]

Output
Matches in text: [ 'quick', 'fox' ]
Matches in array: [ 'banana', 'fox' ]

Summary:

  • Strings: Use RegExp or includes() for flexible searching.
  • Arrays: Use filter() and includes() for efficient matching.
  • Case Sensitivity: Use regular expressions with the i flag for case-insensitive matching.
  • Combine these techniques for more complex searches involving multiple words

Next Article

Similar Reads