The simplest way to do it (if you're not worried about special Unicode characters) is to call toUpperCase:
- When both strings being compared are variables (not constants), then it's a little more complicated 'cause we need to generate a RegExp from the string but passing the string to RegExp constructor can result in incorrect matches or failed matches if the string has special regex characters in it.
- If we care about internationalization don't use toLowerCase() or toUpperCase() as it doesn't provide accurate case-insensitive comparisons in all languages.
Remember that casing is a locale specific operation. Depending on scenario we may want to take that in to account. For example, if we are comparing names of two people we may want to consider locale but if we are comparing machine generated values such as UUID then we might not. This why we use following function in my utils library (note that type checking is not included for performance reason).
With the help of regular expression also we can achieve.
we is for ignore case. If not necessary we can ignore and test for NOT case sensitive match like
There are two ways for case insensitive comparison:
Convert strings to upper case and then compare them using the strict operator (===). How strict operator treats operands read stuff
Pattern matching using string methods:
Use the "search" string method for case insensitive search. Read about search and other string methods