【问题标题】:javascript filter an array of strings, matching case insensitive substringjavascript过滤字符串数组,匹配不区分大小写的子字符串
【发布时间】:2020-07-09 19:54:26
【问题描述】:

我有一个由某个字符串过滤的名称数组。如何匹配包含此字符串但不区分大小写的所有名称?

this.employeeDisplayList = this.employeeList.filter(e => e.employeeName.includes(this.searchStr));

【问题讨论】:

标签: javascript


【解决方案1】:
this.employeeDisplayList = this.employeeList.filter(e => {
    const emp = e.employeeName.toLowerCase();
    const str = this.searchStr.toLowerCase();
    return emp.includes(str);
});

【讨论】:

    【解决方案2】:

    您可以将.toLowerCase() 应用于employeeName 和searchString。

    let employeeDisplayList = [{employeeName: "Jules 1"}, {employeeName: "jules 2"}, {employeeName: "Max"}];
    
    let searchStr = "Jules";
    
    console.log(employeeDisplayList.filter(e => e.employeeName.toLowerCase().indexOf(searchStr.toLowerCase()) >= 0));

    另一种方法是使用不区分大小写的正则表达式进行搜索:string.match(/word/i)

    【讨论】:

    • 显然我来晚了一分钟 XD
    猜你喜欢
    • 2014-09-07
    • 2013-05-03
    • 1970-01-01
    • 2014-10-26
    • 2017-12-09
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多