Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptHow to check whether a string contains a substring in JavaScript?

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

In this article, we will check whether a string contains a specified substring or not. 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 checking whether a string contains a substring in JavaScript.

Approach 1: Using includes() Method:

The 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 neveropen";
 
let flag = str.includes("Geeks");
 
console.log(flag);


Output

true

Approach 2: Using test() Method:

In this method, we will use test() method to test the substring in the string. This method returns true if the substring found else return 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 neveropen";
         
let flag = /neveropen/.test(str);
 
console.log(flag);


Output

true

Approach 3 : Using the match() method with a regular expression:

In this approach, The 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 neveropen";
let substring = "there";
let containsSubstring = str.match(new RegExp(substring)) !== null;
console.log(containsSubstring);


Output

true

Approach 4: Using the indexOf() method

The 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:

Javascript




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


Output

true

Approach 5 : Using the search() method with a regular expression

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: In this example, we are using the above-explained approach.

Javascript




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


Output

true

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments