Open In App

How to check whether a string contains a substring in JavaScript ?

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A substring is a portion of a string. It represents a sequence of characters extracted from a larger string. A substring can be as short as a single character or extend to multiple characters.

There are several approaches to check whether a string contains a substring or not:

Checking for Substring with includes() Method

JavaScript string.includes() method can be used to check whether a string contains a specified substring. It returns true if the substring is present. This method is case-sensitive. 

Syntax:

string.includes(searchvalue, start)

Example: This example uses the includes() method of JavaScript to check for the substring in a string.

Javascript




let str = "Hello there! Welcome to GeeksforGeeks";
let flag = str.includes("Geeks");
console.log(flag);


Output

true



Explanation:

The code checks if the string “Hello there! Welcome to GeeksforGeeks” contains the substring “Geeks”. It assigns the result of this check to the variable `flag` and logs it to the console.

Checking for Substring with test() Method

In this method, we will use the test() method to test the substring in the string. This method returns true if the substring found else returns false. 

Syntax:

/sub-string/.test( String ); 

Example: This example uses the test() method of JavaScript to check for the substring in a string.

Javascript




let str = "Hello there! Welcome to geeksforgeeks";
let flag = /geeks/.test(str);
 
console.log(flag);


Output

true



Explanation:

The code tests whether the string “Hello there! Welcome to geeksforgeeks” contains the substring “geeks” using a regular expression. It assigns the result to flag and logs it to the console.

Checking for Substring with match() Method

In this approach, JavaScript string.match() method with a regular expression checks if a substring exists in the string, returning null if not found or an array of matches if found.

Syntax:

string.match(regExp)

Example: In this example, we are using the above-explained approach.

Javascript




let str = "Hello there! Welcome to geeksforgeeks";
let substring = "there";
let containsSubstring = str.match(new RegExp(substring)) !== null;
console.log(containsSubstring);


Output

true



Explanation:

The code searches for the substring “there” within the string “Hello there! Welcome to geeksforgeeks” using a regular expression. It assigns the result to `containsSubstring` and logs it.

Checking for Substring with indexOf() Method

JavaScript indexOf() method returns the position of a substring in a string or -1 if not found, allowing us to check the substring’s existence.

Syntax:

str.indexOf(searchValue , index);

Example: Here, we are using the above-explained approach.

Javascript




let str = "Hello there! Welcome to geeksforgeeks";
let substring = "geeks";
let result = str.includes(substring);
console.log(result);


Output

true



Explanation:

The code checks if the string “Hello there! Welcome to geeksforgeeks” contains the substring “geeks”. It assigns the result to ‘result’ and logs it.

Checking for Substring with search() Method

In this approach, the search() method is called on the str string with a regular expression object created from the substring value using the new RegExp(substring). It searches for the first occurrence of the substring within the string. By checking if the result is not equal to -1, we determine if the substring exists in the string. The boolean result is stored in the containsSubstring variable and printed to the console.

Syntax:

string.search(new RegExp)

Example: Here, we are using the above-explained approach.

Javascript




let str = "Hello there! Welcome to geeksforgeeks"
let substring = "geeks";
let result = str.search(new RegExp(substring)) !== -1;
console.log(result);


Output

true



Explanation:

A regular expression is created using new RegExp(substring) to match the substring “geeks”. The search() method is used to find the index of substring within str. The boolean result (whether substring is found in str) is assigned to result.

We can check the whether a String Contains a Substring using Loadsh

You can check for substring using Lodash _.strContains() methd, In this approach we can use the lodash _.strContains() method, It will check the presence of he given substring into the given string and return the boolean value.

// Defining lodash contrib variable 
let _ = require('lodash-contrib');

let bool = _.strContains("GeeksforGeeks", "Geeks");

console.log("The String contains the "
    + "searched String : ", bool); 

Note: You need to install lodash-contrib package to run this code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads