javascript compare strings without being case sensitive [duplicate] javascript compare strings without being case sensitive [duplicate] javascript javascript

javascript compare strings without being case sensitive [duplicate]


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}


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/


You can also use string.match().

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