From 11e93fa275dc3a6a42668384f501ede030888441 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Fri, 22 Jul 2022 11:45:21 +0300 Subject: [PATCH] Add includesCaseInsensitive --- snippets/includesCaseInsensitive.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/includesCaseInsensitive.md diff --git a/snippets/includesCaseInsensitive.md b/snippets/includesCaseInsensitive.md new file mode 100644 index 000000000..4054ed5fd --- /dev/null +++ b/snippets/includesCaseInsensitive.md @@ -0,0 +1,22 @@ +--- +title: Case-insensitive substring search +tags: string +expertise: beginner +author: chalarangelo +cover: blog_images/cup-of-orange.jpg +firstSeen: 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. + +```js +const includesCaseInsensitive = (str, searchString) => + new RegExp(searchString, 'i').test(str); +``` + +```js +includesCaseInsensitive('Blue Whale', 'blue'); // true +```