Javascript String Contains: Check If A String Contains A Substring


To check if a Javascript String Contains a substring or not, we can use 7 different Javascript methods as listed in below table.

Javascript string contains MethodJavascript string contains exampleResult
Javascript includes"Javascript String Contains".includes("String")true
Javascript indexOf"Javascript String Contains".indexOf("substring")!== -1;false
Javscript search"Javascript String Contains".search("substring")!== -1;false
Javascript match"Javascript String Contains".match("Contains")!=null
true
Javascript Regular expression/Contains/.test("Javascript String Contains")true
Lodash includes_.includes('lodash substring', 'lodash');true
Underscoreincludes("underscore substring","underscore")true

To check for case-insensitive Javascript string contains or Javascript string contains after some index, go through the complete tutorial.

Table of Contents

1.JavaScript string contains substring check using includes method (in ES6):

If you are using latest versions of Javascript like ES6 you can use include method to check if a string contains a substring or not as shown below.

We will take an example and understand it further.

var substringtocheck = "includes";
var actualstring="ES6 contains includes method";
var isContains = actualstring.includes(substringtocheck);
//returns true.

includes method is case sensitive, so the below substring check returns false

var substringtocheck = "Includes";
var actualstring="ES6 contains includes method";
var isContains = actualstring.includes(substringtocheck);
// returns false

Javascript string includes method accepts two parameters

  1. substring to check
  2. search position

The second parameter search position is optional and the default value is 0.

If you want to check whether a string contains a substring only after a certain position you can pass the search position as a parameter as shown in the below examples.

var substringtocheck = "ES6";
var actualstring="ES6 contains includes method";
var isContains = actualstring.includes(substringtocheck,1);
//returns false. As we are checking for ES6 substring after position 1.

includes method is very easy to use and comes with additional options like search position.

In older versions of Javascript, we can add a polyfill for include method

if (!String.prototype.includes) {

Object.defineProperty(String.prototype, 'includes', {
  value: function(substring, searchposition) {
     if (typeof searchposition!== 'number') {
       searchposition= 0
     }
     if (searchposition+ substring.length > this.length) {
       return false
     } else {
       return this.indexOf(substring, searchposition) !== -1
     }
  }
})
}

2.check if a string contains a substring in javascript (in ES5 and before):

In ES5 and before versions of Javascript we can use indexOf method to check if a string contains a substring or not.

Go through the following code example to understand it further.

var actualstring = "In ES5 use IndexOf method",
var substringToCheck = "IndexOf";
var isContains=actualstring.indexOf(substringToCheck) !== -1;
//isContains true

The indexOf method will return the substring position if it found, otherwise returns -1.

And indexOf method is case sensitive,accepts two parameter similar to includes method.

var actualstring = "In ES5 use IndexOf method",
var substringToCheck = "indexOf";
var isContains=actualstring.indexOf(substringToCheck) !== -1;
//isContains false as
var isContainsAfterPosition = actualstring.indexOf("In",2) !== -1;
//false.

We can simply add pollyfil method for includes as it uses indexOf method internally.

3.Javascript string contains substring check using search method:

Javascript string prototype contains search method which accepts regular expression as parameter.

We can use search method to check if a string contains a substring or not as shown below.

var actualstring = "Javascript search method",
var substringToCheck = "search";
var isContains=actualstring.search("search") !== -1;
//isContains true

The search method in JavaScript will return substring position if found, otherwise -1.

4.check if a string contains a substring or not by using Javascript match method:

To check if a string contains substring or not, use the Javascript match method.

Javascript match method accepts a regular expression as a parameter and if the string contains the given substring it will return an object with details of index, substring, input, and groups.

If the string does not contain the substring then it will return null.

We will take an example to understand it.

var actualstring = "Javascript match method",
var substringToCheck = "match";
actualstring.match("match");
//["match", index: 11, input: "Javascript match method", groups: undefined]

actualstring.match("javascript");
//null

5. Javascript string contains a substring using Regular Expressions:

Using regular expression test method, we can verify if a string contains a substring or not.

We have to create a regular expression for the substring and then test it using the target string.

var actualstring = "RegExp test method";
var regexp = /test/;
regexp.test(actualstring);
//returns true

/RegExp/.test(actualstring);
//returns true

We have to pass our substring as regular expression only (i.e., without quotes) as shown above.

The regular expression test method in javascript will return true or false only.

We cannot get the index of a substring with this method.

check if a string contains substring in Javascript

check if a string contains substring in Javascript

6. Javascript String Contains Case-insensitive check

To check for case-insensitive Javascript string contains, use the below methods.

The simplest way is to convert the entire string to either to lowercase or uppercase and use the javascript indexOf, includes methods as shown below.

var actualstring = "javascript string contains case-insensitive";
var targetstring = "Contains";

if(actualstring.toLowerCase().includes(targetstring.toLowerCase())
{
 //String contains
}

if(actualstring.actualstring.indexOf(targetstring.toLowerCase()) !== -1)
{
 //String contains
}

if(actualstring.toUpperCase().includes(targetstring.toUpperCase())
{ 
 //String contains 
} 

if(actualstring.toUpperCase.indexOf(targetstring.toUpperCase()) !== -1)
{
 //String contains 
}

To check for case insensitive string contains, use the regular expressions. Add suffix “i” after the string regular expression as shown.

var actualstring = "Javascript Regular Exp";
var regexp = "javascript";
/regexp/i.test(actualstring);
//returns true

Javascript search method accepts regular expressions, we can do case insensitive string contains check as shown below.

var actualstring = "Javascript search method";
var isContains=actualstring.search("javascript") !== -1;
// false
actualstring.search(/javascript/i) !== -1;
//true

And also we can use Javascript match method to do case insensitive substring check  as it accepts regular expressions

actualstring.match(/javascript/i)
["Javascript", index: 0, input: "Javascript match method", groups: undefined]

if(!actualstring.match(/javascript/i)){
//Found
}

Or we can use third-party libraries like lodash js and underscore js to check if a string contains a substring or not in javascript.

These libraries contain so many javascript utility methods that can be used in our daily projects.

And lodash and underscore js libraries are well documented and well tested. So we can use them without any worries

7.check if a string contains a substring in javascript using Lodash js:

lodash library contains ._includes() method which is used to check if a string contains another substring or not in javascript.

This method will return true if match found otherwise it will return false

_.includes('lodash substring', 'lodash');
//returns true

8.check if a string contains a substring in javascript using Underscore js:

Similary underscore js contains includes() method and we can pass substring and actual string as parameters, if match found it will return true otherwise false

Syntax of includes method is include(string, substring)

includes("underscore substring","underscore")
//returns true.

9.Best way to check if a string contains a substring or not in javascript:

  1. Using regular expressions to check a simple substring is not advisable due to performence related issues.
  2. lodash and underscore methods requires to load external javascript files.
  3. If speed matters use the simple indexOf method in javascript to check whether a string contains a substring.

Wait before leaving. why can’t you follow me on twitter or be a friend on Facebook or linkedn to get in touch with me.

Spread the word

Get free link to download 900+ Material Icons
Avatar
Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Related

Next
Previous