62

I have to check some strings using JavaScript but case sensitivity is causing problems. for example

if('abc'=='ABC')
{
return true;
}

it will not go inside the if loop though the meaning of the word are same. I cant use tolower clause too since i dont know the data how it would come it means for ex:

if('aBc'=='abC')
{
return true;
}

how to write the JS function for this if it could be done by jquery.

Russell Giddings
  • 8,731
  • 5
  • 34
  • 35
ankur
  • 4,565
  • 14
  • 64
  • 100
  • 1
    The already answered question has so much better info. I suggest you (current reader) ignore this page – Adriano Sep 26 '14 at 09:48

4 Answers4

125

You can make both arguments lower case, and that way you will always end up with a case insensitive search.

var string1 = "aBc";
var string2 = "AbC";

if (string1.toLowerCase() === string2.toLowerCase())
{
    #stuff
}
Gazler
  • 83,029
  • 18
  • 279
  • 245
  • 2
    Note: You need to be careful about just indiscriminately applying this to code as string1 == string2 will return true if both variables are null or false if only one is null. Whereas string1.toLowerCase() == string2.toLowerCase() will throw an error if either variable is null. – Russell Giddings Sep 30 '13 at 20:20
  • 7
    actually, as pointed out in the already answered question, you should NOT use `.toLowerCase()` but `.toUpperCase()`. see http://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison and also http://msdn.microsoft.com/en-us/library/bb386042.aspx – Adriano Sep 26 '14 at 09:47
  • three equals sign? === – Sandeep Jul 21 '16 at 15:15
18

Another method using a regular expression (this is more correct than Zachary's answer):

var string1 = 'someText',
    string2 = 'SometexT',
    regex = new RegExp('^' + string1 + '$', 'i');

if (regex.test(string2)) {
    return true;
}

RegExp.test() will return true or false.

Also, adding the '^' (signifying the start of the string) to the beginning and '$' (signifying the end of the string) to the end make sure that your regular expression will match only if 'sometext' is the only text in stringToTest. If you're looking for text that contains the regular expression, it's ok to leave those off.

It might just be easier to use the string.toLowerCase() method.

So... regular expressions are powerful, but you should only use them if you understand how they work. Unexpected things can happen when you use something you don't understand.

There are tons of regular expression 'tutorials', but most appear to be trying to push a certain product. Here's what looks like a decent tutorial... granted, it's written for using php, but otherwise, it appears to be a nice beginner's tutorial: http://weblogtoolscollection.com/regex/regex.php

This appears to be a good tool to test regular expressions: http://gskinner.com/RegExr/

Akrikos
  • 3,595
  • 2
  • 24
  • 22
  • this one is best, even it is works for null, while toUpperCase or toLowerCase gives error - estupendo – Ali Adravi Sep 03 '15 at 19:44
  • What if string1 is not valid regexp pattern? For eample `new RegExp('^(a$', 'i')` – Bohdan Lyzanets May 04 '16 at 11:40
  • @Bohdan, I'd encourage you to try that sort of thing in the js console. If you do, you'll find that your browser throws a SyntaxError, so I don't think it's relevant in this case. – Akrikos May 05 '16 at 20:07
  • 2
    It's been a few years since I posted this answer. I'd like to note for posterity that I usually use a variation on the toUpperCase answer by Gazler when I need to do a case insensitive comparison. Regular expressions are, in general, slower than other built in methods. – Akrikos May 05 '16 at 20:08
10

You can also use string.match().

var string1 = "aBc";
var match = string1.match(/AbC/i);

if(match) {
}
Zachary
  • 6,522
  • 22
  • 34
  • 1
    Is it also possible to use another variable instead of the /AbC/ part???? – YeppThat'sMe Apr 20 '12 at 13:33
  • YeppThat'sMe: Yes, you could do: var string1 = "aBc", regex = /AbC/i, match = string1.match(regex); Please note that match will be the part of the string that matches a regular expression or null if the regular expression doesn't match anything. I'll post an alternate answer that uses a different regex method to do this. – Akrikos May 25 '12 at 14:21
  • 5
    You should use `/^abc$/i.test("aBc")` because without the anchor at start and end it's really saying **contains**, not **equals**. – John Leidegren Aug 05 '12 at 09:45
5

Try this...

if(string1.toLowerCase() == string2.toLowerCase()){
    return true;
}

Also, it's not a loop, it's a block of code. Loops are generally repeated (although they can possibly execute only once), whereas a block of code never repeats.

I read your note about not using toLowerCase, but can't see why it would be a problem.

belugabob
  • 4,302
  • 22
  • 22