Ian Herbert
Ian Herbert

Reputation: 1019

Javascript + regex badword filter that ignores whitespace and other characters within the word

Well, I thought this would be simple but I am having problems converting some PHP regex code to javascript.

Here is my JS code:

for(var i=0; i < badwords.length; i++) {
    var badword = badwords[i].slice(0, -2);
    var regex = "/(";

    for(var j=0; j < badword.length; j++) {
        regex += "[" + badword[j].toLowerCase() + "|" + badword[j].toUpperCase() + "][/W]*";
    }
    regex += ")/";

    msg = msg.replace(new RegExp(regex, "g"), "");
}

I am not getting a match and no replacement is happening. This same code structure and regex format worked in a PHP application.

Any help is appreciated, thanks.

Upvotes: 0

Views: 2863

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272296

If your input is foo, your regular expression should be /f[^a-z]*o[^a-z]*o/ig. You can build this regular expression using strings and regular expressions like this:

var message = 'this string should not contain "foo", "b a r", "b.l.a.h" and "b_a_z". Baaz is acceptable.';
var badwords = ["foo", "bar", "blah", "baz"];
for (var i = 0; i < badwords.length; i++) {
    var pat = badwords[i].slice(0, -1).replace(/([a-z])/g, "$1[^a-z]*") + badwords[i].slice(-1);
    var rxp = new RegExp(pat, "ig");
    message = message.replace(rxp, "****");
}
message;
// output:
// this string should not contain "****", "****", "****" and "****". Baaz is acceptable.

Upvotes: 1

Related Questions