javascript - How to do case insensitive string comparison?

ID : 954

viewed : 106

Tags : javascriptstringjavascript





Top 5 Answer for javascript - How to do case insensitive string comparison?

vote vote

94

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(); 
vote vote

84

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

vote vote

76

As said in recent comments, string::localeCompare supports case insensitive comparisons (among other powerful things).

Here's a simple example

'xyz'.localeCompare('XyZ', undefined, { sensitivity: 'base' }); // returns 0 

And a generic function you could use

function equalsIgnoringCase(text, other) {     return text.localeCompare(other, undefined, { sensitivity: 'base' }) === 0; } 

Note that instead of undefined you should probably enter the specific locale you are working with. This is important as denoted in the MDN docs

in Swedish, ä and a are separate base letters

Sensitivity options

Sensitivity options tabulated from MDN

Browser support

As of time of posting, UC Browser for Android and Opera Mini do not support locale and options parameters. Please check https://caniuse.com/#search=localeCompare for up to date info.

vote vote

61

Update:

As per the comments, previous answer checks for source contains keyword, to make it equality check added ^ and $.

(/^keyword$/i).test(source) 

With the help of regular expression also we can achieve.

(/keyword/i).test(source) 

/i is for ignoring case. If not necessary we can ignore and test for NOT case sensitive matches like

(/keyword/).test(source) 
vote vote

55

Remember that casing is a locale specific operation. Depending on scenario you may want to take that in to account. For example, if you are comparing names of two people you may want to consider locale but if you are comparing machine generated values such as UUID then you might not. This why I use following function in my utils library (note that type checking is not included for performance reason).

function compareStrings (string1, string2, ignoreCase, useLocale) {     if (ignoreCase) {         if (useLocale) {             string1 = string1.toLocaleLowerCase();             string2 = string2.toLocaleLowerCase();         }         else {             string1 = string1.toLowerCase();             string2 = string2.toLowerCase();         }     }      return string1 === string2; } 

Top 3 video Explaining javascript - How to do case insensitive string comparison?







Related QUESTION?