Contains case insensitive

409,047

Solution 1

Add .toUpperCase() after referrer. This method turns the string into an upper case string. Then, use .indexOf() using RAL instead of Ral.

if (referrer.toUpperCase().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

Solution 2

Another options is to use the search method as follow:

if (referrer.search(new RegExp("Ral", "i")) == -1) { ...

It looks more elegant then converting the whole string to lower case and it may be more efficient.
With toLowerCase() the code have two pass over the string, one pass is on the entire string to convert it to lower case and another is to look for the desired index.
With RegExp the code have one pass over the string which it looks to match the desired index.

Therefore, on long strings I recommend to use the RegExp version (I guess that on short strings this efficiency comes on the account of creating the RegExp object though)

Solution 3

From ES2016 you can also use slightly better / easier / more elegant method (case-sensitive):

if (referrer.includes("Ral")) { ... }

or (case-insensitive):

if (referrer.toLowerCase().includes(someString.toLowerCase())) { ... }

Here is some comparison of .indexOf() and .includes(): https://dev.to/adroitcoder/includes-vs-indexof-in-javascript

Solution 4

Use a RegExp:

if (!/ral/i.test(referrer)) {
    ...
}

Or, use .toLowerCase():

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

Solution 5

There are a couple of approaches here.

If you want to perform a case-insensitive check for just this instance, do something like the following.

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

Alternatively, if you're performing this check regularly, you can add a new indexOf()-like method to String, but make it case insensitive.

String.prototype.indexOfInsensitive = function (s, b) {
    return this.toLowerCase().indexOf(s.toLowerCase(), b);
}

// Then invoke it
if (referrer.indexOfInsensitive("Ral") == -1) { ...
Share:
409,047
Nate Pet
Author by

Nate Pet

Updated on March 25, 2021

Comments

  • Nate Pet
    Nate Pet over 3 years

    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?

  • Domenic
    Domenic over 12 years
    +1, this could potentially be more correct by avoiding the "Turkish I problem" and other such pitfalls: i18nguy.com/unicode/turkish-i18n.html
  • Domenic
    Domenic over 12 years
    The latter method is more correct; the former will fail for the Turkish I and any other such problematic uppercase/lowercase pairs: i18nguy.com/unicode/turkish-i18n.html
  • Rob W
    Rob W over 12 years
    For modern browsers which support defineProperty, I suggest Object.defineProperty(String.prototype, 'indexOfInsensitive', {value: function(s,b){return this.toLowerCase().indexOf((s+'').toLowerCase(),b);}});. Two updates: Explicit string conversion using (s+''), and non-enumerable in a loop (for(var i in '') ... does not show indexOfInsensitive.
  • Mottie
    Mottie almost 12 years
    For Turkish, it would be better to use toLocaleLowerCase() (ref)
  • Nicolas Fall
    Nicolas Fall almost 11 years
    the latter doesn't answer the question, it only says if it is there, not get the index of the match. Either the question title is wrong, or the question.
  • Rob W
    Rob W almost 11 years
    @Maslow The question's example was about testing case insensivity. If you want to get the index, use the String's .search method: var index = referrer.search(/Ral/i);
  • zachelrath
    zachelrath over 10 years
    The added complication of the dynamic Regular Expression approach is that if the search string, e.g. "Ral", contained Regular Expression special characters, such as $.*? etc., you'd have problems, so you would need to escape the special characters, see Mike Samuel's answer on this post: endsWith in JavaScript
  • Nateowami
    Nateowami almost 10 years
    As pointed out by others elsewhere, it's better to use toUpperCase(). See msdn.microsoft.com/en-us/library/bb386042.aspx
  • Ilan Biala
    Ilan Biala about 9 years
    This is also quite a bit faster based on my tests: jsperf.com/case-insensitive-indexof
  • Mr world wide
    Mr world wide over 6 years
    Or you can simply do like this: if(referrer.toLowerCase().indexOf("Ral".toLowerCase()) === -1) {
  • Kind Contributor
    Kind Contributor about 6 years
    @RolandIllig Ouch. My answer doesn't accommodate other cultures, that's a drawback. I would welcome any insight into broadening support for more cultures, the world is a better place with collaborators.
  • nixkuroi
    nixkuroi over 5 years
    As of 2018.10.24, toLowerCase wins by a large margin in Chrome. toLowerCase (95,914,378 - ±0.89% - fastest), regex indexOf (269,307 - ±0.87% 100% slower)
  • Kyle s
    Kyle s over 5 years
    I don't think includes is case insensitive
  • drzaus
    drzaus over 5 years
    @Kyles includes is case-sensitive in Chrome: try 'fooBar'.includes('bar') ==> false
  • Dan Dascalescu
    Dan Dascalescu about 5 years
    @Domenic: With all due respect to the Turkish culture, Turkey should consider a spelling reform to simplify this aspect. China has had a number of simplification reforms, and Turkey has less than 10% China's population, and a much simpler alphabet. It can be done.
  • Pradeep Kumar
    Pradeep Kumar over 3 years
    I got tolowercase is not a function error (when I used referrer value from input field). This fixed: if (referrer.toString().toLowerCase().indexOf("ral") === -1) {
  • Danon
    Danon about 3 years
    How is that "better"?
  • Jeph
    Jeph almost 3 years
    It seems JSPerf is dead now, so I've recreated the test on JSBenchme: jsbench.me/bckqv6ii1c/1 As of 2021 on a M1 Macbook Air Regex is 99.43% slower than indexOf.
  • Wannes
    Wannes almost 3 years
    For the performance geeks, using RegExp.test was faster on my machine using the same benchmark. So in the case of this example: (new RegExp('Ral', 'i')).test(referrer)
  • Teepeemm
    Teepeemm almost 3 years
    @ PradeepKumar It's not clear if you needed .toString(), or if you needed the capital L in .toLowerCase().
  • Chipsy
    Chipsy over 2 years
    Wow. I found it so so useful. Thank you for this.