hankknight
asked on
JavaScript: Case insensitive test against each item in array
I use the code below to test a string against each item in array. (Thanks, sjklein42)
Now I want it to be case insensitive.
Now I want it to be case insensitive.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript">
/*<![CDATA[*/
Array.prototype.contains = function(obj) {
// Thanks, sjklein42
var i = this.length;
while (i--) {
if (obj.indexOf(this[i].toLowerCase()) != (-1))
{
return true;
}
}
return false;
}
var badStuff = new Array("aaa","bbb","ccc","ddd","EEE");
var v = 'AAA hello world';
alert(badStuff.contains(v)); // should alert true because v contains aaa and aaa is in the baddStuff array
var v = 'This is a bbbbbbbb test';
alert(badStuff.contains(v)); // should alert true because v contains bbb and bbb is in the baddStuff array
var v = 'Hello World';
alert(badStuff.contains(v)); // should alert false
var v = 'CCC';
alert(badStuff.contains(v)); // should alert true
/*]]>*/
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window