Javascript 数字和字符的自定义解析器
大家好,我想写一个类似于下面的解析器,除了我希望它使用像Javascript 数字和字符的自定义解析器,javascript,jquery,parsing,Javascript,Jquery,Parsing,大家好,我想写一个类似于下面的解析器,除了我希望它使用像345j,982p0这样的数字字符。我要做什么改变才能让字符带有数字 ts.addParser({ id: "digit", is: function (s, table) { var c = table.config; return $.tablesorter.isDigit(s, c); }, format: function (s) { return $.
345j
,982p0
这样的数字字符。我要做什么改变才能让字符带有数字
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"
});
假设您希望允许字母和/或数字的任意组合,但不允许其他组合,则可以使用正则表达式:
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-z
和0-9
的任意组合,不区分大小写。我还更改了格式函数,因为您无法将其作为浮点进行解析,并更改名称和类型以反映解析器中的更改。是否希望“bobjones”有效?那么“bobj0nes”呢?请编辑您的问题并将源代码放入代码块中。突出显示源代码,然后按ctrl-shift-k,或者单击匹配的大括号按钮{}
。另外,是否特别希望允许数字和字母的任意组合?标点或空格呢?@Alex:只是ctrl-k。