Contains case insensitive
Let's use .toLowerCase()
or .toUpperCase()
in tandem with .includes()
method for case-insensitivity:
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:
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?
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:
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!
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:
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!
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:
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?
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!
Was this article helpful?