Contains case insensitive
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
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)
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
Use a RegExp:
if (!/ral/i.test(referrer)) {
...
}
Or, use .toLowerCase()
:
if (referrer.toLowerCase().indexOf("ral") == -1)