开发者

JavaScript SPAM Word Filter

HI

I am trying to use Javascript to write a simple SPAM word filter that loops through an array of words and tries to match the whole word that is a passed in as string.

Below is what I have so far and it works except that is does partial word matching instead of matching the whole word.

So in my example below the string passed in below:

We are Offering Great Education Classes and Many CE Credits all Year Long!

Matched the word "credit"

I am looking for a way to match the whole word and not a partial word match.

Any help would be appreciated.

    var spam_words_arr=new Array(
"loan",
"winning",
"bulk email",
"mortgage",
"free",
"save",
"credit",
"amazing",
"bulk",
"email",
"opportunity",
"please read",
"reverses aging",
"hidden assets",
"stop snoring",
"free investment",
"dig up dirt on friends",
"stock disclaimer statement",
"multi level marketing",
"compare rates",
"cable converter",
"claims you can be removed from t开发者_StackOverflow中文版he list",
"removes wrinkles",
"compete for your business",
"free installation",
"free grant money",
"auto email removal",
"collect child support",
"free leads",
"amazing stuff",
"tells you it's an ad",
"cash bonus",
"promise you",
"claims to be in accordance with some spam law",
"search engine listings",
"free preview",
"act now! don't hesitate",
"credit bureaus",
"no investment",
"obligation",
"guarantee",
"refinance",
"price",
"affordable",
"home loan",
"lower your monthly payments",
"new low rate",
"Your Mortgage",
"Your refi",
"serious cash"); 



 function SubChecker() { 
    var sSubject = document.form1.subject.value;
    reset_alert_count();
    var alert_title = "The following words and phrases are not recommended in subject lines";
    var compare_text; 

        eval('compare_text=sSubject;'); 
            for(var j=0; j<spam_words_arr.length; j++) { 
                for(var k=0; k<(compare_text.length); k++) { 
                    if(spam_words_arr[j]==compare_text.substring(k,(k+spam_words_arr[j].length)).toLowerCase()) {
                        spam_alert_arr[spam_alert_count]=compare_text.substring(k,(k+spam_words_arr[j].length)); 
                        spam_alert_count++; 
                    } 
                } 
        } 
        for(var k=1; k<=spam_alert_count; k++) { 
            alert_text+= "<br> <li> "+ spam_alert_arr[k-1]; 
            eval('compare_text=document.form1.subject.focus();'); 
            eval('compare_text=document.form1.subject.select();'); 
        } 

    } 

OK Here is my revision but I cannot get the code to run. Can someone take a look and give me hand with some suggestions.

Thanks in advance.

function SubChecker() { 
var sSubject = document.form1.subject.value;
reset_alert_count();
var alert_title = "The following words and phrases are not recommended in subject lines";


    for(var j=0; j<spam_words_arr.length; j++) {
            for(var k=0; k<(sSubject.length); k++) {
                var rExp = new RegExp("("+spam_words_arr[j]+")", "ig");
                alert(rExp);
                if(rExp.match(sSubject)){
                    spam_alert_count++;
                }
    }
    for(var k=1; k<=spam_alert_count; k++) {
        alert_text+= "<br> <li> "+ spam_alert_arr[k-1];

    }



enter code here


You could make your array of "words" an array of regular expressions, and the the \b word boundary marker. E.g.:

var spam_words_arr=new Array(
    /\bloan\b/i,
    ...
);

...then use the exec or test functions on the regular expression to do the test.

In fact, your array could become one massive alternation with \b on either end:

var regex = /\b(?:loan|winning|bulk email|mortgage|free)\b/i;

(I've obviously left most of the array out.) In a JavaScript regular expression, an alternation like a|b means "match a or b.

Another advantage to using a regular expression for this is that you can be more flexible than a brute-force list of all suspect words.


Off-topic:

  1. For initializing an array, I'd recommend array literal notation rather than the constructor call you've used, e.g.:

    var spam_words_array = [
        entry,
        entry,
        entry,
        // ...
    ];
    

    It's shorter, it can't run afoul of someone redefining Array, and you don't have the ambiguity of what var x = new Array(5); should mean (that creates an array with five blank spots, rather than an array with one entry containing 5).

  2. Those uses of eval are...odd as they seem completely unnecessary. There are very, very few use-cases where eval is necessary (I've managed to do several years of JavaScript coding without ever using it in production code). If you find yourself writing eval, recommend posting a question here on StackOverflow with just the bit of code you think you need it for, and why, and the folks here will give you a better alternative.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜