filterBadWords

by Jenna Smith

JavaScript

String.prototype.filterBadWords = (function(){
    var filter = ['whore', 'piss', 'shit', 'fuck', 'cunt', 'bastard', 'twat', 'wank', 'bullshit', 'tit', 'wanker', 'bollocks'],
        regex = new RegExp('\\b' + filter.join('\\b|\\b') + '\\b', 'gi');

    return function(){
        return this.replace(regex, function(m){
            return (new Array(m.length + 1)).join('*');
        });
    };
}());

var test = 'what the fuck is going on?'.filterBadWords();
document.write(test);