Contains case insensitive

Nate Pet picture Nate Pet · Jan 24, 2012 · Viewed 355.9k times · Source

I have the following:

if (referrer.indexOf("Ral") == -1) { ... }

What I like to do is to make Ral case insensitive, so that it can be RAl, rAl, etc. and still match.

Is there a way to say that Ral has to be case-insensitive?

Answer

Rob W picture Rob W · Jan 24, 2012

Add .toLowerCase() after referrer. This method turns the string in a lower case string. Then, use .indexOf() using ral instead of Ral.

if (referrer.toLowerCase().indexOf("ral") === -1) { 

The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):

if (!/Ral/i.test(referrer)) {
   //    ^i = Ignore case flag for RegExp