How to Check If String Contains a Substring in JavaScript?

In JavaScript, to check if a string contains a substring, you can use any of the following:

Please note that finding a substring in a string means that the string "are", for example, will be matched even when it's found as a part of a word such as "fare", "dare", etc. This is an important distinction to draw between checking for a substring within a string and checking for a word within a string.

Using String.prototype.includes()

Introduced in ES6, you can use the String.prototype.includes() method to check for the presence of a substring in a string, for example, like so:

// ES6+
const str = 'Hello World!';

console.log(str.includes('World')); // true
console.log(str.includes('world')); // false

As you can see in the example above, a boolean value is returned indicating whether the match is found or not, and the matches are case-sensitive.

You can also specify the optional second argument to the method to specify the position in the string to start searching the substring from, for example, like so:

// ES6+
const str = 'Hello World!';

console.log(str.includes('Hello', 1)); // false
console.log(str.includes('ello', 1)); // true

Using String.prototype.indexOf()

The String.prototype.indexOf() method is actually intended for finding the index of the first occurrence of the substring, or -1 if no match is found. Even though, finding a substring within a given string is not its primary use case, you can still use it for that purpose, for example, in the following way:

const str = 'Hello World!';

console.log(str.indexOf('hello') > -1); // false
console.log(str.indexOf('Hello') > -1); // true
console.log(str.indexOf('!') > -1); // true

As you can see in the example above, the String.prototype.indexOf() method performs a case-sensitive search. Comparing it against -1 ensures that the return value is always a boolean value.

Using String.prototype.search()

The String.prototype.search() method is similar in terms of return value and functionality to the String.prototype.indexOf() method, with the exception that the former accepts strings as well as regular expressions as input. This means that, similar to using the String.prototype.indexOf() method, if match is found, the position of the first occurrence in the string is returned, or -1 otherwise:

const str = 'Hello World!';

console.log(str.search('hello') > -1); // false
console.log(str.search('Hello') > -1); // true
console.log(str.search('!') > -1); // true

You can perform the same matches using regular expression literal notation or by calling the constructor function of the RegExp object:

const str = 'Hello World!';

// regular expression literal notation
console.log(str.search(/hello/) > -1); // false
console.log(str.search(/Hello/) > -1); // true
console.log(str.search(/!/) > -1); // true

// calling the constructor function of the RegExp object
console.log(str.search(new RegExp('hello')) > -1); // false
console.log(str.search(new RegExp('Hello')) > -1); // true
console.log(str.search(new RegExp('!')) > -1); // true

By default, this will perform a case-sensitive search, however, you can specify the i flag to do a case-insensitive search instead:

const str = 'Hello World!';

// regular expression literal notation
console.log(str.search(/hello/i) > -1); // true

// calling the constructor function of the RegExp object
console.log(str.search(new RegExp('hello', 'i')) > -1); // true

Using RegExp.prototype.test()

The RegExp.prototype.test() method accepts a regular expression as an argument, and returns a boolean value if a match is found in the string:

const str = 'Hello World!';

// regular expression literal notation
console.log(/hello/.test(str)); // false
console.log(/Hello/.test(str)); // true
console.log(/!/.test(str)); // true

// calling the constructor function of the RegExp object
console.log((new RegExp('hello')).test(str)); // false
console.log((new RegExp('Hello')).test(str)); // true
console.log((new RegExp('!')).test(str)); // true

By default, this will perform a case-sensitive search, however, you can specify the i flag to do a case-insensitive search instead:

const str = 'Hello World!';

// regular expression literal notation
console.log(/hello/i.test(str)); // true

// calling the constructor function of the RegExp object
console.log((new RegExp('hello', 'i')).test(str)); // true

Hope you found this post useful. It was published (and was last revised ). Please show your love and support by sharing this post.