3 ways of Check subString contains in a string JavaScript

Multiple ways to check string contains substring in javascript

How to check if a substring exists in a string in Javascript

String checking operations is a basic operation every developer needs to know. This post will help you through several ways of checking whether a substring exists in a given string in JavaScript.

  • ES5 String indexOf() method, that returns index if found, else -1.
  • Inbuilt String search() method, returns positional index if found, else return -1
  • Using ES6 String includes() method, that returns true if found, else false.

How to Check if a substring exists in a string ES5 String indexOf method

Before ES6, the indexOf method was used to check the substring presence in the main string.

It returns -1 if the string is not found, or else the positional index of the first occurrence of a given substring.

Syntax:

String.indexOf(substring);

Input and return values

  • input is given substring to check the presence
  • returns number -1 or greater than 0.

Example:

let mainString = "String checking indexOf example";
console.log(mainString.indexOf("checking")); // 7
console.log(mainString.indexOf("asdfad")); // -1

To check if a substring exists in a string, we need to compare the indexOf result with -1 using if conditional expression.

if ("first string".indexOf("string") == -1) {
  console.log("not found");
} else {
  console.log("found");
}

From the above example, code works, but is not human readable, and conditional expression always needs an extra check against integers instead of simply true or false values.

So checking integer values in a conditional expression, is a disadvantage

How to Check if a substring exists in a String javascript Search with regular expression

string.search() is another way of checking string exists or not. We can pass regular expressions as a parameter.

It checks the regular expression against a given string object and returns the positional index, else not found, returns -1.

Syntax:

String.search(regular expression)

the return type is an index of matched starting positions. input accepted is a regular expression.

let string = "search string example";
console.log(string.search("string") !== -1); // true
console.log(string.search("string") !== -1); // true
console.log(string.search("string")); // outputs 7

The disadvantage is regular expression processing takes more time and resources, It is not advisable to use this for simple use cases, moreover not readable.

How to Check if a substring exists using ES6 String includes a method

ES6 introduced string includes() a new method that returns true, if a given substring contains a string, else return false.

Syntax

String.includes(substring, index);

Parameters

  • substring - This is string text that checks for existence in a given string
  • index - starting position at which checking starts, and optional
  • Returns true - if substring found false - not found in a given string Here is an example.
const message = "Substring checking example";

console.log(message.includes("example")); // true
console.log(message.includes("aserwerew")); // false
console.log(message.includes("as", 8)); // false
console.log(message.includes("string", 3)); // true

Conclusion

To summarize, Learned multiple ways to check if a substring exists in a string.