js string contains substring ignore case 1

js string contains substring ignore case

const str = 'arya stark';

// The most concise way to check substrings ignoring case is using
// `String#match()` and a case-insensitive regular expression (the 'i')
str.match(/Stark/i); // true
str.match(/Snow/i); // false

// You can also convert both the string and the search string to lower case.
str.toLowerCase().includes('Stark'.toLowerCase()); // true
str.toLowerCase().indexOf('Stark'.toLowerCase()) !== -1; // true

str.toLowerCase().includes('Snow'.toLowerCase()); // false
str.toLowerCase().indexOf('Snow'.toLowerCase()) !== -1; // false

Here is what the above code is Doing:
1. `str.toLowerCase()` converts the string to lower case.
2. `’Stark’.toLowerCase()` converts the search string to lower case.
3. `str.toLowerCase().includes(‘Stark’.toLowerCase())` checks if the lower case
string includes the lower case search string.
4. `str.toLowerCase().indexOf(‘Stark’.toLowerCase()) !== -1` checks if the lower
case string includes the lower case search string.

Similar Posts