Files
30-seconds-of-code/snippets/includes-case-insensitive.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

597 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
Case-insensitive substring search string chalarangelo cup-of-orange 2022-07-28T05:00:00-04:00

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 given searchString, 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