hankknight
asked on
JavaScript: Test string against each item in array
I have a JavaScript array containing a list of blacklisted words.
I want to return true if a string contains any of the blacklisted words.
I want to return true if a string contains any of the blacklisted words.
<!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) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
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.