javascript compare strings without being case sensitive

JavascriptJqueryJquery Plugins

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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
}

Solution 2 - Javascript

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/

Solution 3 - Javascript

You can also use string.match().

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

if(match) {
}

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionankurView Question on Stackoverflow
Solution 1 - JavascriptGazlerView Answer on Stackoverflow
Solution 2 - JavascriptAkrikosView Answer on Stackoverflow
Solution 3 - JavascriptZacharyView Answer on Stackoverflow
Solution 4 - JavascriptbelugabobView Answer on Stackoverflow