EN
JavaScript - string contains case insensitive
0
points
In this article, we would like to show you how to check if a string contains a word (case insensitive) in JavaScript.
1. Check if the string contains the word
In this example, we convert whole text string to lowercase, then we search for the word (also in lowercase) to find out if the word is present in the string (true) or not (false).
// ONLINE-RUNNER:browser;
const text = 'This IS Example text...';
// convert everything to lowercase
const textLowercase = text.toLowerCase(); // this is example text...
// check the condition
const result = textLowercase.includes('example');
console.log(result); // true
2. Find the index of the word
This example is similar to the first one, but in this one we use indexOf() method to find the index of the word that we search for.
// ONLINE-RUNNER:browser;
const text = 'This IS Example text...';
// convert everything to lowercase
const textLowercase = text.toLowerCase(); // this is example text...
// find index
const result = textLowercase.indexOf('example');
console.log(result); // 8