Skip to content

Commit

Permalink
Add includesCaseInsensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Jul 22, 2022
1 parent a75d0c4 commit 11e93fa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions snippets/includesCaseInsensitive.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 11e93fa

Please sign in to comment.