Explain Codes LogoExplain Codes Logo

Contains case insensitive

javascript
prompt-engineering
functions
callbacks
Anton ShumikhinbyAnton Shumikhin·Sep 2, 2024
TLDR

Let's use .toLowerCase() or .toUpperCase() in tandem with .includes() method for case-insensitivity:

let contains = "JavaScript".toLowerCase().includes("script".toLowerCase()); // Even "script" has a case of an identity crisis! // returns true

The beauty of this method is that it converts both strings to a uniform case, so .includes() can forget about case differences and focus on its job.

A buffet of alternative methods

The good-old JavaScript buddy RegExp

Ever dreamed of having flexibility in your searches? Your dream just came true! Use a regular expression with the i flag for case-insensitive searches:

let pattern = /script/i; let containsRegEx = pattern.test("JavaScript"); // "JavaScript", "JAVAscript", "jaVAsCRipt" - It's all the same to me! // returns true

Custom indexOfInsensitive function for the nth time

For repeated use, compact the behavior into a function. Because, why do the same work again and again?

function indexOfInsensitive(haystack, needle) { return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1; // I don't care how you spell it as long as I find it! }

includes to the rescue

If you only need to check if something exists or not, .includes() over .indexOf() is your guardian angel. It returns a boolean and is super readable:

let containsIncludes = "JavaScript".includes("script"); // I see you! // returns true

Performance jargon

Time is money and RegExp knows it! For long strings or many searches, RegExp may be as slow as a snail. Use string methods for simple substrings and reserve RegExp for complex patterns.

RegExp: The secret weapon for flexible searching

Reusing patterns with RegExp: Save time, reuse!

Refactor common patterns by creating a regexp factory. Don't forget, RegExp is a powerful ally!

function createCaseInsensitiveRegexp(str) { return new RegExp(str, 'i'); // I've got you a ticket to the case-insensitive club! } const pattern = createCaseInsensitiveRegexp('script'); // Uniform case, here we come!

A test you'll love: test() method

Are you tired of coding? Take a little break and use the test() method that gives you a quick yes/no answer:

let isMatch = createCaseInsensitiveRegexp('script').test("JavaScript"); // I feel like a match-maker! // returns true

Modify prototypes with caution: Handle with care!

Extending built-in prototypes can let you sleep in peace with custom methods. But beware, it may lead to conflicts or issues with third-party libraries. So, thread carefully!

String.prototype.includesCaseInsensitive = function(searchString) { return this.toLowerCase().includes(searchString.toLowerCase()); // I don't see any case, do you? };

Hosting a coding party with more applications and pitfalls

Special characters in royalty with Regex

Give importance to special characters and complex matching that is beyond simple substring checks:

let hasSpecialChars = /[@#]/i.test('Hello#World'); // Special characters are just like everyone else! // returns true

No more discrimination: Case conversion for all!

Everybody deserves to be treated equally. Isn't it nice when equality makes an appearance in your code as well?

let sameCase = 'JavaScript'.toLowerCase() === 'JAVAscript'.toLowerCase(); // You say Potato, I say potato! // returns true

The key to organisation and clean code

Take this golden opportunity to your advantage and refactor common tasks into reusable methods or functions. Not only does it clean up your code but also makes it look fancy!