数字和字符的自定义解析器

时间:2011-01-06 20:57:13

标签: javascript jquery parsing

您好我想编写一个类似下面的解析器,除了我希望它使用345j982p0之类的数字。我可以改变什么才能拥有带数字的字符?

ts.addParser({
    id: "digit",
    is: function (s, table) {
        var c = table.config;
        return $.tablesorter.isDigit(s, c);
    },
    format: function (s) {
        return $.tablesorter.formatFloat(s);
    },
    type: "numeric"
});

1 个答案:

答案 0 :(得分:1)

假设你想允许任何字母和/或数字的组合,但没有别的,你可以使用正则表达式:

ts.addParser({
    id: "alphanumeric",
    is: function(s, table) {
        return /^[a-z0-9]*$/i.test(s);
    },
    format: function(s) {
        return s;
    },
    type: "text"
});

正则表达式为/^[a-z0-9]*$/i,仅匹配a-z0-9的任意组合,不区分大小写。我也改变了你的格式函数,因为你不能将它解析为float,并更改名称和类型以反映解析器中的更改。

相关问题