如何使用nodejs使搜索案例不敏感



我有一个搜索功能,因此我从客户端Str输入了,如果与文件中的内容匹配,请在响应中发送该功能。假设我现在在文件中有文本Lorem中的文本,如果我从客户端搜索为lorem,它会因为案件敏感而发送空数组。如何使搜索案例不敏感?

searchService.js

var searchStr;
function readFile(str, logFiles, callback) {
        searchStr = str;
        // loop through each file
        async.eachSeries(logFiles, function (logfile, done) {
            // read file
            fs.readFile('logs/dit/' + logfile.filename, 'utf8', function (err, data) {
                if (err) {
                    return done(err);
                }
                var lines = data.split('n'); // get the lines
                lines.forEach(function(line) { // for each line in lines
                    if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt
                        results.push({
                        filename:logfile.filename,
                        value:line
                        });
                    }
                });
                // when you are done reading the file
                done();
            });

您可以使用toLowerCase()

if (line.toLowerCase().indexOf(searchStr.toLowerCase()) != -1) { ...

您可以使用.match(/datterion/i)使用正则。/i使模式搜索案例不敏感。

if ("LOREMMMMMM".match(/Lorem/i)) console.log("Match");

最新更新