Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript compare strings without being case sensitive [duplicate]

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.

like image 616
ankur Avatar asked Feb 07 '11 08:02

ankur


People also ask

How do you compare strings without case-sensitive?

The equalsIgnoreCase() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Is JavaScript string comparison case-sensitive?

A general requirement while working in javascript is case-insensitive string comparisons.

Which method is used to compare two strings ignoring cases?

equalsIgnoreCase() method compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Can we use == to compare strings in JavaScript?

In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.


1 Answers

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 } 
like image 102
Gazler Avatar answered Sep 24 '22 02:09

Gazler