jQuery Data Type How to - Compare and exclude string keywords from an array list








Question

We would like to know how to compare and exclude string keywords from an array list.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--   ww w  .  j a v a  2s .c o m-->
var keywords = "Did the cow jump over the moon?";
var exclusionlist = ["a","is","of","in","on","it","to","if","so","the","i","we","did"];
var result = keywords
    .match(/\w+/g)
    .map(function(i) {
        return i.toLowerCase();}
    )
    .filter(function(i) {
        return exclusionlist.indexOf(i) == -1; }
    );
document.writeln(result);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: