jquery tablesorter - 用空格排序数字

时间:2013-01-30 14:50:02

标签: jquery tablesorter

我有jQuery tablesorter和12 345 678,91

这样的数字的问题
$(document).ready(function() {
    $.tablesorter.addParser({ 
        id: 'thousands',
        is: function(s) { 
            return false; 
        }, 
        format: function(s) {
            return s.replace(' ','').replace(/,/g,'');
        }, 
        type: 'numeric' 
    }); 

    $("#tablesorter").tablesorter({
    headers: { 
                3: { sorter:'thousands' }, 
                4: { sorter:'thousands' }, 
                5: { sorter:'thousands' } 
            }
    });
});

输出过滤器:

-1 295,76
-331,2
-330,01
-290
0
3 986 495,06
1 942 503,09
0
0

当我替换它时:s.replace('','')。replace(/,/ g,''); 通过这个:s.replace(new RegExp(/ [^ 0-9 / A-Za-z。] / g),“”); ......比现在还要糟糕。 任何想法?

3 个答案:

答案 0 :(得分:3)

解析器不能替换所有空格;使用replace(' ','')只会替换第一个空格。此外,逗号应替换为小数点,因为它表示分数。所以,试试这个(demo):

$.tablesorter.addParser({
    id: 'thousands',
    is: function (s) {
        return false;
    },
    format: function (s) {
        return s.replace(/\s+/g, '').replace(/,/g, '.');
    },
    type: 'numeric'
});

答案 1 :(得分:0)

Internet Explorer将空格转换为 。 对我来说,这是:

return s.replace(/ /g,'').replace(/\s+/g, '')

答案 2 :(得分:0)

@ Triple_6:

在实施解决方案之前了解其含义: 我的猜测是你不想要空格,或者只是纯数字。

下面的表达式意味着使用""替换数字,字母,小数点和空格以外的所有内容。

s.replace(new RegExp(/[^0-9/A-Za-z. ]/g),"");

以上是我的建议。

<强>解决方案:

如果您不想要空格,请从表达式中删除它。

s.replace(new RegExp(/[^0-9/A-Za-z.]/g),"");

如果您想拥有该标志,请将

包括在内
s.replace(new RegExp(/[^0-9/A-Za-z.+-]/g),"");

正则表达式的含义: 新的RegExp(/ [除此列表] / g),&#34;用这个&#34;)替换一切;

工作jsfiddle: http://jsfiddle.net/rK5s4/1/

下次请务必提及您希望多次删除某些内容。你的问题并不清楚。

在合并之前,请尝试将它们放入jsfiddle。

相关问题