Reputation: 1019
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
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