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.
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
).
xxxxxxxxxx
1
const text = 'This IS Example text...';
2
3
// convert everything to lowercase
4
const textLowercase = text.toLowerCase(); // this is example text...
5
6
// check the condition
7
const result = textLowercase.includes('example');
8
9
console.log(result); // true
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.
xxxxxxxxxx
1
const text = 'This IS Example text...';
2
3
// convert everything to lowercase
4
const textLowercase = text.toLowerCase(); // this is example text...
5
6
// find index
7
const result = textLowercase.indexOf('example');
8
9
console.log(result); // 8