[javascript] How to check if a string contains text from an array of substrings in JavaScript?

Pretty straight forward. In javascript, I need to check if a string contains any substrings held in an array.

This question is related to javascript arrays string substring contains

The answer is


There's nothing built-in that will do that for you, you'll have to write a function for it.

If you know the strings don't contain any of the characters that are special in regular expressions, then you can cheat a bit, like this:

if (new RegExp(substrings.join("|")).test(string)) {
    // At least one match
}

...which creates a regular expression that's a series of alternations for the substrings you're looking for (e.g., one|two) and tests to see if there are matches for any of them, but if any of the substrings contains any characters that are special in regexes (*, [, etc.), you'd have to escape them first and you're better off just doing the boring loop instead. For info about escaping them, see this question's answers.

Live Example:

_x000D_
_x000D_
var substrings = ["one", "two", "three"];
var str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}
_x000D_
_x000D_
_x000D_


In a comment on the question, Martin asks about the new Array.prototype.map method in ECMAScript5. map isn't all that much help, but some is:

if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    // There's at least one
}

Live Example:

_x000D_
_x000D_
var substrings = ["one", "two", "three"];
var str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}
_x000D_
_x000D_
_x000D_

You only have it on ECMAScript5-compliant implementations, though it's trivial to polyfill.


Update in 2020: The some example can be simpler with an arrow function (ES2015+), and you might use includes rather than indexOf:

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

Live Example:

_x000D_
_x000D_
const substrings = ["one", "two", "three"];
let str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}
_x000D_
_x000D_
_x000D_

Or even throw bind at it, although for me the arrow function is much more readable:

if (substrings.some(str.includes.bind(str))) {
    // There's at least one
}

Live Example:

_x000D_
_x000D_
const substrings = ["one", "two", "three"];
let str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(str.includes.bind(str))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(str.includes.bind(str))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}
_x000D_
_x000D_
_x000D_


If you're working with a long list of substrings consisting of full "words" separated by spaces or any other common character, you can be a little clever in your search.

First divide your string into groups of X, then X+1, then X+2, ..., up to Y. X and Y should be the number of words in your substring with the fewest and most words respectively. For example if X is 1 and Y is 4, "Alpha Beta Gamma Delta" becomes:

"Alpha" "Beta" "Gamma" "Delta"

"Alpha Beta" "Beta Gamma" "Gamma Delta"

"Alpha Beta Gamma" "Beta Gamma Delta"

"Alpha Beta Gamma Delta"

If X would be 2 and Y be 3, then you'd omit the first and last row.

Now you can search on this list quickly if you insert it into a Set (or a Map), much faster than by string comparison.

The downside is that you can't search for substrings like "ta Gamm". Of course you could allow for that by splitting by character instead of by word, but then you'd often need to build a massive Set and the time/memory spent doing so outweighs the benefits.


Not that I'm suggesting that you go and extend/modify String's prototype, but this is what I've done:

String.prototype.includes()

_x000D_
_x000D_
String.prototype.includes = function (includes) {_x000D_
    console.warn("String.prototype.includes() has been modified.");_x000D_
    return function (searchString, position) {_x000D_
        if (searchString instanceof Array) {_x000D_
            for (var i = 0; i < searchString.length; i++) {_x000D_
                if (includes.call(this, searchString[i], position)) {_x000D_
                    return true;_x000D_
                }_x000D_
            }_x000D_
            return false;_x000D_
        } else {_x000D_
            return includes.call(this, searchString, position);_x000D_
        }_x000D_
    }_x000D_
}(String.prototype.includes);_x000D_
_x000D_
console.log('"Hello, World!".includes("foo");',          "Hello, World!".includes("foo")           ); // false_x000D_
console.log('"Hello, World!".includes(",");',            "Hello, World!".includes(",")             ); // true_x000D_
console.log('"Hello, World!".includes(["foo", ","])',    "Hello, World!".includes(["foo", ","])    ); // true_x000D_
console.log('"Hello, World!".includes(["foo", ","], 6)', "Hello, World!".includes(["foo", ","], 6) ); // false
_x000D_
_x000D_
_x000D_


For full support (additionally to @ricca 's verions).

_x000D_
_x000D_
wordsArray = ['hello', 'to', 'nice', 'day']_x000D_
yourString = 'Hello. Today is a nice day'.toLowerCase()_x000D_
result = wordsArray.every(w => yourString.includes(w))_x000D_
console.log('result:', result)
_x000D_
_x000D_
_x000D_


If the array is not large, you could just loop and check the string against each substring individually using indexOf(). Alternatively you could construct a regular expression with substrings as alternatives, which may or may not be more efficient.


Using underscore.js or lodash.js, you can do the following on an array of strings:

var contacts = ['Billy Bob', 'John', 'Bill', 'Sarah'];

var filters = ['Bill', 'Sarah'];

contacts = _.filter(contacts, function(contact) {
    return _.every(filters, function(filter) { return (contact.indexOf(filter) === -1); });
});

// ['John']

And on a single string:

var contact = 'Billy';
var filters = ['Bill', 'Sarah'];

_.every(filters, function(filter) { return (contact.indexOf(filter) >= 0); });

// true

Best answer is here: This is case insensitive as well

    var specsFilter = [.....];
    var yourString = "......";

    //if found a match
    if (specsFilter.some((element) => { return new RegExp(element, "ig").test(yourString) })) {
        // do something
    }

This is super late, but I just ran into this problem. In my own project I used the following to check if a string was in an array:

["a","b"].includes('a')     // true
["a","b"].includes('b')     // true
["a","b"].includes('c')     // false

This way you can take a predefined array and check if it contains a string:

var parameters = ['a','b']
parameters.includes('a')    // true

Javascript function to search an array of tags or keywords using a search string or an array of search strings. (Uses ES5 some array method and ES6 arrow functions)

// returns true for 1 or more matches, where 'a' is an array and 'b' is a search string or an array of multiple search strings
function contains(a, b) {
    // array matches
    if (Array.isArray(b)) {
        return b.some(x => a.indexOf(x) > -1);
    }
    // string match
    return a.indexOf(b) > -1;
}

Example usage:

var a = ["a","b","c","d","e"];
var b = ["a","b"];
if ( contains(a, b) ) {
    // 1 or more matches found
}

substringsArray.every(substring=>yourBigString.indexOf(substring) === -1)

For full support ;)


function containsAny(str, substrings) {
    for (var i = 0; i != substrings.length; i++) {
       var substring = substrings[i];
       if (str.indexOf(substring) != - 1) {
         return substring;
       }
    }
    return null; 
}

var result = containsAny("defg", ["ab", "cd", "ef"]);
console.log("String was found in substring " + result);

For people Googling,

The solid answer should be.

const substrings = ['connect', 'ready'];
const str = 'disconnect';
if (substrings.some(v => str === v)) {
   // Will only return when the `str` is included in the `substrings`
}

var yourstring = 'tasty food'; // the string to check against


var substrings = ['foo','bar'],
    length = substrings.length;
while(length--) {
   if (yourstring.indexOf(substrings[length])!=-1) {
       // one of the substrings is in yourstring
   }
}

convert_to_array = function (sentence) {
     return sentence.trim().split(" ");
};

let ages = convert_to_array ("I'm a programmer in javascript writing script");

function confirmEnding(string) {
let target = "ipt";
    return  (string.substr(-target.length) === target) ? true : false;
}

function mySearchResult() {
return ages.filter(confirmEnding);
}

mySearchResult();

you could check like this and return an array of the matched words using filter


var str = "texttexttext";
var arr = ["asd", "ghj", "xtte"];
for (var i = 0, len = arr.length; i < len; ++i) {
    if (str.indexOf(arr[i]) != -1) {
        // str contains arr[i]
    }
}

edit: If the order of the tests doesn't matter, you could use this (with only one loop variable):

var str = "texttexttext";
var arr = ["asd", "ghj", "xtte"];
for (var i = arr.length - 1; i >= 0; --i) {
    if (str.indexOf(arr[i]) != -1) {
        // str contains arr[i]
    }
}

Here's what is (IMO) by far the best solution. It's a modern (ES6) solution that:

  • is efficient (one line!)
  • avoids for loops
  • unlike the some() function that's used in the other answers, this one doesn't just return a boolean (true/false)
  • instead, it either returns the substring (if it was found in the array), or returns undefined
  • goes a step further and allows you to choose whether or not you need partial substring matches (examples below)

Enjoy!



const arrayOfStrings = ['abc', 'def', 'xyz'];
const str = 'abc';
const found = arrayOfStrings.find(v => (str === v));

Here, found would be set to 'abc' in this case. This will work for exact string matches.

If instead you use:

const found = arrayOfStrings.find(v => str.includes(v));

Once again, found would be set to 'abc' in this case. This doesn't allow for partial matches, so if str was set to 'ab', found would be undefined.


And, if you want partial matches to work, simply flip it so you're doing:
const found = arrayOfStrings.find(v => v.includes(str));

instead. So if str was set to 'ab', found would be set to 'abc'.

Easy peasy!




building on T.J Crowder's answer

using escaped RegExp to test for "at least once" occurrence, of at least one of the substrings.

_x000D_
_x000D_
function buildSearch(substrings) {_x000D_
  return new RegExp(_x000D_
    substrings_x000D_
    .map(function (s) {return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');})_x000D_
    .join('{1,}|') + '{1,}'_x000D_
  );_x000D_
}_x000D_
_x000D_
_x000D_
var pattern = buildSearch(['hello','world']);_x000D_
_x000D_
console.log(pattern.test('hello there'));_x000D_
console.log(pattern.test('what a wonderful world'));_x000D_
console.log(pattern.test('my name is ...'));
_x000D_
_x000D_
_x000D_


One line solution

substringsArray.some(substring=>yourBigString.includes(substring))

Returns true\false if substring exists\does'nt exist

Needs ES6 support


You can check like this:

<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
         var list = ["bad", "words", "include"] 
         var sentence = $("#comments_text").val()

         $.each(list, function( index, value ) {
           if (sentence.indexOf(value) > -1) {
                console.log(value)
            }
         });
         });
      </script>
   </head>
   <body>
      <input id="comments_text" value="This is a bad, with include test"> 
   </body>
</html>

Drawing from T.J. Crowder's solution, I created a prototype to deal with this problem:

Array.prototype.check = function (s) {
  return this.some((v) => {
    return s.indexOf(v) >= 0;
  });
};

let obj = [{name : 'amit'},{name : 'arti'},{name : 'sumit'}];
let input = 'it';

Use filter :

obj.filter((n)=> n.name.trim().toLowerCase().includes(input.trim().toLowerCase()))

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to substring

Go test string contains substring How does String substring work in Swift Delete the last two characters of the String Split String by delimiter position using oracle SQL How do I check if a string contains another string in Swift? Python: Find a substring in a string and returning the index of the substring bash, extract string before a colon SQL SELECT everything after a certain character Finding second occurrence of a substring in a string in Java Select query to remove non-numeric characters

Examples related to contains

Check if element is in the list (contains) Excel formula to search if all cells in a range read "True", if not, then show "False" How to use regex in XPath "contains" function R - test if first occurrence of string1 is followed by string2 Java List.contains(Object with field value equal to x) Check if list contains element that contains a string and get that element Search for "does-not-contain" on a DataFrame in pandas String contains another two strings Java - Opposite of .contains (does not contain) String contains - ignore case