How do I perform case insensitive string comparison in JavaScript?
Case-insensitive string comparison in C++ Here the logic is simple. We will convert the whole string into lowercase or uppercase strings, then compare them, and return the result. We have used the algorithm library to get the transform function to convert the string into lowercase string.
The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the i flag.
Yes, == is case sensitive.
Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.
EDIT: This answer was originally added 9 years ago. Today you should use localeCompare
with the sensitivity: 'accent'
option:
function ciEquals(a, b) { return typeof a === 'string' && typeof b === 'string' ? a.localeCompare(b, undefined, { sensitivity: 'accent' }) === 0 : a === b; } console.log("'a' = 'a'?", ciEquals('a', 'a')); console.log("'AaA' = 'aAa'?", ciEquals('AaA', 'aAa')); console.log("'a' = 'á'?", ciEquals('a', 'á')); console.log("'a' = 'b'?", ciEquals('a', 'b'));
The { sensitivity: 'accent' }
tells localeCompare()
to treat two variants of the same base letter as the same unless they have different accents (as in the third example) above.
Alternatively, you can use { sensitivity: 'base' }
, which treats two characters as equivalent as long as their base character is the same (so A
would be treated as equivalent to á
).
Note that the third parameter of localeCompare
is not supported in IE10 or lower or certain mobile browsers (see the compatibility chart on the page linked above), so if you need to support those browsers, you'll need some kind of fallback:
function ciEqualsInner(a, b) { return a.localeCompare(b, undefined, { sensitivity: 'accent' }) === 0; } function ciEquals(a, b) { if (typeof a !== 'string' || typeof b !== 'string') { return a === b; } // v--- feature detection return ciEqualsInner('A', 'a') ? ciEqualsInner(a, b) : /* fallback approach here */; }
Original answer
The best way to do a case insensitive comparison in JavaScript is to use RegExp match()
method with the i
flag.
Case-insensitive search
When both strings being compared are variables (not constants), then it's a little more complicated 'cause you 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 you care about internationalization don't use toLowerCase()
or toUpperCase()
as it doesn't provide accurate case-insensitive comparisons in all languages.
http://www.i18nguy.com/unicode/turkish-i18n.html
The simplest way to do it (if you're not worried about special Unicode characters) is to call toUpperCase
:
var areEqual = string1.toUpperCase() === string2.toUpperCase();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With