我试图得到一个不区分大小写的搜索与JavaScript工作的两个字符串。

通常是这样的:

var string="Stackoverflow is the BEST";
var result= string.search(/best/i);
alert(result);

/i标志不区分大小写。

但我需要寻找第二根弦;如果没有国旗,它就完美了:

var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(searchstring);
alert(result);

如果我在上面的例子中添加/ I标志,它将搜索searchstring,而不是变量“searchstring”中的内容(下一个例子不工作):

var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(/searchstring/i);
alert(result);

我怎样才能做到这一点呢?


当前回答

我喜欢@CHR15TO的答案,不像我在其他类似问题上看到的其他答案,这个答案实际上展示了如何正确地转义用户提供的搜索字符串(而不是说它是必要的,而没有显示如何)。

然而,它仍然相当笨拙,而且可能相对较慢。那么,为什么不针对程序员的常见需求制定一个具体的解决方案呢?(顺便说一句,为什么不把它包含在ES6 API中呢?)

我对类似问题的回答[https://stackoverflow.com/a/38290557/887092]使以下内容成为可能:

var haystack = 'A. BAIL. Of. Hay.';
var needle = 'bail.';
var index = haystack.naturalIndexOf(needle);

其他回答

我注意到,如果用户输入了一个文本字符串,但在没有选择任何自动完成选项的情况下离开输入,则隐藏输入中不会设置任何值,即使字符串与数组中的字符串重合。 所以,在其他答案的帮助下,我做出了这个:

var $local_source = [{
        value: 1,
        label: "c++"
    }, {
        value: 2,
        label: "java"
    }, {
        value: 3,
        label: "php"
    }, {
        value: 4,
        label: "coldfusion"
    }, {
        value: 5,
        label: "javascript"
    }, {
        value: 6,
        label: "asp"
    }, {
        value: 7,
        label: "ruby"
    }];
    $('#search-fld').autocomplete({
        source: $local_source,
        select: function (event, ui) {
            $("#search-fld").val(ui.item.label); // display the selected text
            $("#search-fldID").val(ui.item.value); // save selected id to hidden input
            return false;
        },
        change: function( event, ui ) {

            var isInArray = false;

            $local_source.forEach(function(element, index){

                if ($("#search-fld").val().toUpperCase() == element.label.toUpperCase()) {
                    isInArray = true;
                    $("#search-fld").val(element.label); // display the selected text
                    $("#search-fldID").val(element.value); // save selected id to hidden input
                    console.log('inarray: '+isInArray+' label: '+element.label+' value: '+element.value);
                };

            });

            if(!isInArray){

                $("#search-fld").val(''); // display the selected text
                $( "#search-fldID" ).val( ui.item? ui.item.value : 0 );

            }
        } 

你可以让所有东西都小写:

var string="Stackoverflow is the BEST";
var searchstring="best";
var result= (string.toLowerCase()).search((searchstring.toLowerCase()));
alert(result);

是的,使用。match,而不是。search。match调用的结果将返回匹配的实际字符串本身,但它仍然可以用作布尔值。

var string = "Stackoverflow is the BEST";
var result = string.match(/best/i);
// result == 'BEST';

if (result){
    alert('Matched');
}

使用这样的正则表达式可能是JavaScript中最整洁、最明显的方法,但请记住,它是正则表达式,因此可能包含正则元字符。如果你想从其他地方获取字符串(例如,用户输入),或者如果你想避免转义大量元字符,那么你可能最好像这样使用indexOf:

matchString = 'best';
// If the match string is coming from user input you could do
// matchString = userInput.toLowerCase() here.

if (string.toLowerCase().indexOf(matchString) != -1){
    alert('Matched');
}

取代

var result= string.search(/searchstring/i);

with

var result= string.search(new RegExp(searchstring, "i"));

ES6 +:

let string="Stackoverflow is the BEST";
let searchstring="best";


let found = string.toLowerCase()
                  .includes(searchstring.toLowerCase());

includes()如果searchString出现在一个或多个位置则返回true,否则返回false。