如何避免返回区分大小写

时间:2015-02-26 08:23:42

标签: javascript jquery

我使用以下代码,它仅适用于案例衰减值, 如果用户使用大写或小写,我想要一些如何克服它 它会起作用,我该怎么做?

oAc.setFilterFunction(function(sValue, oItem) {
    debugger;

    var val = (oItem.getText().indexOf(sValue) != -1) || (oItem.getAdditionalText().indexOf(sValue) != -1);

    return val;

});

在sValue中我得到了值并且val返回true或false 在oItem.getText()中我得到的文本要比较,如果我进入sValue = i 并且oItem.getText()返回索引方法返回true。

3 个答案:

答案 0 :(得分:2)

一种简单的方法是在indexOf()操作

之前将两个值转换为大写/小写
oAc.setFilterFunction(function (sValue, oItem) {
    debugger;
    sValue = sValue.toLowerCase();
    var val = (oItem.getText().toLowerCase().indexOf(sValue) != -1) || (oItem.getAdditionalText().toLowerCase().indexOf(sValue) != -1);

    return val;

});

另一种解决方案是使用正则表达式来测试值,而不是使用indexOf

if (!RegExp.escape) {
    RegExp.escape = function (value) {
        return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
    };
}

oAc.setFilterFunction(function (sValue, oItem) {
    debugger;
    var regex = new RegExp(RegExp.escape(sValue), 'i');
    var val = regex.test(oItem.getText()) || regex.test(oItem.getAdditionalText());
    return val;
});

答案 1 :(得分:0)

您可以将文字转换为小写。

oAc.setFilterFunction(function(sValue, oItem) {
    debugger;

    var val = (oItem.getText().toLowerCase().indexOf(sValue.toLowerCase()) != -1) || (oItem.getAdditionalText().toLowerCase().indexOf(sValue.toLowerCase()) != -1);

    return val;

})

答案 2 :(得分:-2)

您可以在名为toLowerCase()或toUpperCase()的String类中查看Java的内置函数

此处有更多信息

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html