How do I perform case insensitive string comparison in JavaScript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- how to split a list into a given number of sub-lis
As said in recent comments,
string::localCompare
supports case insensitive comparisons (among other powerful things).Here's a simple example
And a generic function you could use
Note that instead of
undefined
you should probably enter the specific locale you are working with. This is important as denoted in the MDN docsSensitivity options
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.
EDIT: This answer is 7 years old. Today you should use
localeCompare
.Original answer
The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the 'i' flag.
JavaScript: 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
How about NOT throwing exceptions and NOT using slow regex?
The above snippet assumes you don't want to match if either string is null or undefined.
If you want to match null/undefined, then:
If for some reason you care about undefined vs null:
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).
With the help of regular expression also we can achieve.
/i
is for ignore case. If not necessary we can ignore and test for NOT case sensitive match likeSuppose we want to find the string variable
needle
in the string variablehaystack
. There are three gotchas:string.toUpperCase
andstring.toLowerCase
. Use a regular expression which ignores case instead. For example,var needleRegExp = new RegExp(needle, "i");
followed byneedleRegExp.test(haystack)
.needle
. Be careful thatneedle
does not contain any regular expression special characters. Escape these usingneedle.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
.needle
andhaystack
, just ignoring case, make sure to add"^"
at the start and"$"
at the end of your regular expression constructor.Taking points (1) and (2) into consideration, an example would be: