How to check if string contains substring in JavaScript

May 02, 2023#javascript#how-to

Checking whether a string contains a specific substring is a common operation in programming. It involves searching for a sequence of characters within a larger string and determining whether it is present or not. This operation can be useful for tasks such as validating user input, searching for keywords in text, or filtering data based on specific criteria.

There are several methods available in JavaScript that can be used to check if a string contains a substring, including:

Using the includes() method

One of the most simple and popular ways is to use the string includes() method, which was introduced in ES6 and works in all modern browsers except Internet Explorer. This method returns true if the string contains the specified substring. Otherwise, it returns false.

let str = 'MongoDB, Express, React, Node'
str.includes('MongoDB') //true
str.includes('Java') //false
str.includes('Node') //true

Note that includes() is case-sensitive and accepts an optional second parameter, an integer, which indicates the position where to start searching for.

Using the indexOf() method

Another way is to use the string indexOf() method, which returns the index of the first occurrence of the substring. If the string does not contain the given substring, it returns -1. The indexOf() method is case-sensitive and accepts two parameters. The first parameter is the substring to search for, and the second optional parameter is the index to start the search from (the default index is 0).

let str = 'MongoDB, Express, React, Node'
str.indexOf('MongoDB') !== -1 // true
str.indexOf('Java') !== -1 // false
str.indexOf('Node', 5) !== -1 // true

Using regular expressions

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. You can create a regular expression in one of two ways: using a regular expression literal, which consists of a pattern enclosed between slashes, or calling the constructor function of the RegExp object.

You can use regular expressions with various string methods:

  1. Using search() to find the position of a substring that matches a regular expression.
let str = "Hello world!";
let re = /world/;
let pos = str.search(re); // returns 6
  1. Using test() to test for a match in a string and return true or false.
let str = "The best things in life are free!";
let re = /e/;
let result = re.test(str); // returns true
  1. Using match() to get an array of substrings that match a regular expression.
let str = "The rain in Spain falls mainly in the plain";
let re = /ain/g;
let result = str.match(re); // returns ["ain", "ain", "ain", "ain"]

Use regular expressions when you need to search for complex patterns within a string. Regular expressions provide powerful and flexible pattern matching capabilities.

Using utility libraries

There are some 3rd party libraries that provide utility functions for checking substring in string, such as Lodash, Underscore, or Voca. These libraries offer methods that can help you perform substring operations more easily and consistently. Here is an example of using Lodash to check substring in string:

// Using Lodash to check if a string contains a substring
var str = "Hello world!";
var result = _.includes(str, "world"); // returns true

// Using Lodash to check if a string starts with a substring
var str = "Hello world!";
var result = _.startsWith(str, "Hello"); // returns true

// Using Lodash to check if a string ends with a substring
var str = "Hello world!";
var result = _.endsWith(str, "!"); // returns true

// Using Lodash to find the index of the first occurrence of a substring
var str = "Hello world!";
var result = _.indexOf(str, "o"); // returns 4

You should consider using these libraries when you need to perform more complex or frequent string operations in your code. However, keep in mind that using external libraries can increase the size and complexity of your codebase, so make sure to balance the benefits with the costs.