How To Make Includes() Case Insensitive In JavaScript?

make includes() case insensitive in JavaScript

Includes() is a built-in string function in JavaScript. It is used to strictly check whether a string exists in a given string or not. Follow this article to know how to make includes() case insensitive in JavaScript.

What is Includes()?

Syntax:

includes(searchString, position)

Parameters:

  • searchString: any string to be checked for within the string that call includes(). It cannot be a regular expression.
  • position (optional): the place in the string where to begin checking for searchString.

Includes() is a case-sensitive search. Returns true if it detects the searchString in the given string; otherwise, it returns false.

Make includes() case insensitive in JavaScript

There are many ways to make includes() case insensitive in JavaScript, and here we will show you the three most simple ways.

Method 1: Using toLowerCase() or toUpperCase()

toLowerCase() syntax:

str.toLowerCase()

Description:

toLowerCase() returns a string converted to lowercase based on the given str.

toUpperCase() syntax:

str.toUpperCase()

Description:

toUpperCase() returns a string converted to uppercase based on the given str.

We can combine includes() with these two methods to make includes() case insensitive in JavaScript. Like this:

let givenStr = 'LeArnSHAreiT.COM';
let searchStr = 'learnshareit';

//use toLowerCase()
if (givenStr.toLowerCase().includes(searchStr.toLowerCase())) {
  console.log(`${searchStr} exists within ${givenStr}`);
} else {
  console.log(`${searchStr} does not exists within ${givenStr}`);
}

//use toUpperCase()
if (givenStr.toUpperCase().includes(searchStr.toUpperCase())) {
  console.log(`${searchStr} exists within ${givenStr}`);
} else {
  console.log(`${searchStr} does not exists within ${givenStr}`);
}

Output:

learnshareit exists within LeArnSHAreiT.COM
learnshareit exists within LeArnSHAreiT.COM

This is the simplest way to make includes() case insensitive in JavaScript. Remember, just use 1 of the two methods above.

Method 2: Using regex and test() method

Syntax:

regex.test(str)

Parameters:

  • regex: a search value that can be a regular expression.
  • str: a given string. 

The test() method returns true if it finds a match in a given string. Otherwise, it returns false.

We can manually build a function similar to includes() case insensitive. Like this:

let givenStr = 'LeArnSHAreiT.COM';
let searchStr = 'learnshareit';

const insensitiveIncludes = function (givenStr, searchStr) {
  let regex = new RegExp(searchStr, 'i');

  if (regex.test(givenStr)) {
    console.log(`${searchStr} exists within ${givenStr}`);
  } else {
    console.log(`${searchStr} does not exists within ${givenStr}`);
  }
};

insensitiveIncludes(givenStr, searchStr);

Output:

learnshareit exists within LeArnSHAreiT.COM

Now we can replace includes() with insensitiveIncludes() for the entire application.

Summary

So far, we have discovered two ways to make includes() case insensitive in JavaScript. Depending on each case, you can choose the most suitable way. Thanks for reading. If you have any problems, don’t hesitate to leave a comment below.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *