Case-insensitive substring search
JavaScript, String · Jul 28, 2022

Checks if a string contains a substring, case-insensitive.
- Use the
RegExp
constructor with the'i'
flag to create a regular expression, that matches the givensearchString
, ignoring the case. - Use
RegExp.prototype.test()
to check if the string contains the substring.
const includesCaseInsensitive = (str, searchString) =>
new RegExp(searchString, 'i').test(str);
includesCaseInsensitive('Blue Whale', 'blue'); // true