javascript includes() case insensitive

前端 未结 6 1418
臣服心动
臣服心动 2020-12-14 05:07

lets say I have an array of filter strings that I need to loop and check against with other passed in string.

var filterstrings = [\'firststring\',\'secondst         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 06:09

    My option is comparing UPPER with UPPER or lower with lower transforming both sides (i did it often in SQL):

        var filterstrings = ['firststring','secondstring','thirDstrIng'];
        var passedinstring =  'ThIrDsTrInG3';
        
        //used for of just to make it more readable
        for (filterstring of filterstrings) {
            if (passedinstring.toUpperCase().includes(filterstring.toUpperCase())) {
                alert("string detected");
            }
        
        }
    
    

    Prompts string detected

提交回复
热议问题