Case-insensitive substring search

Angelos Chalaris · 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 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

Recommended snippets

  • String is anagram

    JavaScript, String

    Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).

  • String ends with substring

    JavaScript, String

    Checks if a given string ends with a substring of another string.

  • Checks if a given string starts with a substring of another string.