Contains case insensitive
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)
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