Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contains case insensitive

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?

like image 420
Nate Pet Avatar asked Jan 24 '12 20:01

Nate Pet


People also ask

Is contain case-sensitive?

Contains() method in C# is case sensitive. And there is not StringComparison parameter available similar to Equals() method, which helps to compare case insensitive.

Is contains in Python case-sensitive?

Note that the Python string contains() method is case sensitive.

Is contains case-sensitive in JavaScript?

Both String#includes() and String#indexOf() are case sensitive. Neither function supports regular expressions. To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.


1 Answers

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 
like image 154
Rob W Avatar answered Sep 27 '22 21:09

Rob W