Case-insensitive substring search
Checks if a string contains a substring, case-insensitive.
- Use the
RegExpconstructor with the
'i'flag to create a regular expression, that matches the given
searchString, ignoring the case.
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
Written by Angelos Chalaris
If you want to keep in touch, follow me on GitHub or Twitter.