Home > Web Front-end > JS Tutorial > body text

How to Efficiently Check for Substring Matches from an Array in JavaScript?

Mary-Kate Olsen
Release: 2024-10-31 09:03:29
Original
160 people have browsed it

How to Efficiently Check for Substring Matches from an Array in JavaScript?

String Matching with Array Substrings in JavaScript

Problem: How to efficiently determine if a given string contains any substring from a predefined array in JavaScript?

Solution:

There are two approaches you can take for this task:

1. Array.some() Method:

The array.some() method allows you to check if at least one element in the array satisfies a given condition. You can utilize it as follows:

<code class="javascript">if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
  // There's at least one matching substring
}</code>
Copy after login

Or, with arrow functions and the includes() method:

<code class="javascript">if (substrings.some(v => str.includes(v))) {
  // There's at least one matching substring
}</code>
Copy after login

2. Regular Expression:

While there's no built-in function specifically designed for this type of search, you can construct a regular expression to match the desired substrings and use the test() method on the string. However, this approach can be computationally expensive for large arrays and strings.

<code class="javascript">const regex = new RegExp(`( ${substrings.join(' | ')} )`, 'g');
if (regex.test(str)) {
  // There's at least one matching substring
}</code>
Copy after login

Example:

Consider the following array of substrings and two different strings:

<code class="javascript">const substrings = ["one", "two", "three"];

let str1 = "this has one";
let str2 = "this doesn't have any";</code>
Copy after login

Using the array.some() approach:

<code class="javascript">if (substrings.some(v => str1.includes(v))) {
  console.log(`Match using "${str1}"`);
} else {
  console.log(`No match using "${str1}"`);
}</code>
Copy after login

Output:

Match using "this has one"
Copy after login
<code class="javascript">if (substrings.some(v => str2.includes(v))) {
  console.log(`Match using "${str2}"`);
} else {
  console.log(`No match using "${str2}"`);
}</code>
Copy after login

Output:

No match using "this doesn't have any"
Copy after login

The above is the detailed content of How to Efficiently Check for Substring Matches from an Array in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!