2

我正在尝试为我的 ajax 聊天系统编写一个小助手类,我正在努力添加我可能需要的基本功能。

var strings = {
        filterWords: ["fool", "dumb", "arse"],
        removeSpecialChars: function (str) {
            return str.replace(/[^\w\s]/gi, '');
        },
        killSpace: function (str) {
            return str.replace(/\s/g, '');
        },
        reduceSpace: function (str) {
            return str.replace(/\s+/g, ' ');
        },
        allowLetsAndNums: function (str) {
            return str.replace(/[^A-Za-z0-9]/g, ' ');
        },
        allowLets: function (str) {
            return str.replace(/[^A-Za-z]/g, ' ');
        },
        allowNums: function (str) {
            return str.replace(/[^0-9]/g, ' ');
        },
        wordFilter: function (str) {
            var rgx = new RegExp(this.filterWords.join("|"), "gi");
            return str.replace(rgx, "****");
        }
    }

我发现我可能需要一起运行多种方法我在问最好的做法是什么而不导致以下结果?

alert(strings.wordFilter(strings.reduceSpace(strings.allowLets("efgwge @£235%^@£ fool you a dumb arse432345$%^"))));

谢谢

4

4 回答 4

3

你可以使它成为一个流畅的界面,允许这样的代码:

var x = new Validation("efgwge @£235%^@£ fool you a dumb arse432345$%^");
alert(x.allowLets().reduceSpace().wordFilter().result());
// alerts "efgwge **** you a **** ****"

您的主要代码需要是:

var Validation = function(str) {
    this.str = str;
    filterWords = ["fool", "dumb", "arse"]
    this.removeSpecialChars = function () {
        this.str = this.str.replace(/[^\w\s]/gi, '');
        return this;
    };    
    this.killSpace = function () {
        this.str = this.str.replace(/\s/g, '');
        return this;
    };
    this.reduceSpace = function () {
        this.str = this.str.replace(/\s+/g, ' ');
        return this;
    };
    this.allowLetsAndNums = function () {
        this.str = this.str.replace(/[^A-Za-z0-9]/g, ' ');
        return this;
    };
    this.allowLets = function () {
        this.str = this.str.replace(/[^A-Za-z]/g, ' ');
        return this;
    };
    this.allowNums = function () {
        this.str = this.str.replace(/[^0-9]/g, ' ');
        return this;
    };
    this.wordFilter = function () {
        var rgx = new RegExp(filterWords.join("|"), "gi");
        this.str = this.str.replace(rgx, "****");
        return this;
    };
    this.result = function(){
        return this.str;
    };
}

现场示例:http: //jsfiddle.net/fb7en/

于 2012-11-07T10:39:22.287 回答
1

您可以扩展 String 原型:

String.prototype.removeSpecialChars = function () {
return this.replace(/[^\w\s]/gi, '');
}
String.prototype.killSpace = function () {
return this.replace(/\s/g, '');
}

var foo = "This is my§$% String";
​document.write​(foo.removeSpecialChars​().killSpace());​
于 2012-11-07T10:34:53.407 回答
1

您可以将函数添加到 String.prototype 中,这样您就可以像这样调用函数:

String.prototype.killSpace = function() {
  return this.replace(/\s/g, '');
}
String.prototype.reduceSpace = function () {
  return this.replace(/\s+/g, ' ');
}

"foo   bar".reduceSpace().killSpace(); // => returns foobar

唯一的缺点是您不能使用 for..in 循环遍历字符串,因为它会将方法列为成员,并且目前没有跨浏览器的方式使其不可迭代(IE 不支持它)。

于 2012-11-07T10:39:57.337 回答
0

您可能会考虑为您的对象使用可链接的 API:

var StringFilter = {
    _string: '',
    string: function (string) {
        this._string = string || '';
        return this;
    },
    filterWords: ["fool", "dumb", "arse"],
    removeSpecialChars: function () {
        this._string = this._string.replace(/[^\w\s]/gi, '');
        return this;
    },
    killSpace: function () {
        this._string = this._string.replace(/\s/g, '');
        return this;
    },
    reduceSpace: function () {
        this._string = this._string.replace(/\s+/g, ' ');
        return this;
    },
    allowLetsAndNums: function () {
        this._string = this._string.replace(/[^A-Za-z0-9]/g, ' ');
        return this;
    },
    allowLets: function () {
        this._string = this._string.replace(/[^A-Za-z]/g, ' ');
        return this;
    },
    allowNums: function () {
        this._string = this._string.replace(/[^0-9]/g, ' ');
        return this;
    },
    wordFilter: function () {
        var rgx = new RegExp(this.filterWords.join("|"), "gi");
        this._string = this._string.replace(rgx, "****");
        return this;
    },
    select: function () {
        return this._string;
    }
};

StringFilter
    .string("efgwge @£235%^@£ fool you a dumb arse432345$%^")
    .allowLets()
    .reduceSpace()
    .wordFilter()
    .select();
于 2012-11-07T10:46:00.347 回答