简体   繁体   English

如何使用nodejs使搜索不区分大小写?

[英]How to make search case insensitive using nodejs?

I have a search function so i have input from client Str if that matchs with content in file send that in response. 我有一个搜索功能,因此我可以从客户端Str输入,如果与文件中的内容匹配,则以响应形式发送。 Lets assume i have text in file Lorem now if i search as lorem from client, it sends empty array because of case sensitive. 假设我现在从客户端搜索lorem ,现在我在Lorem文件中有文本,由于区分大小写,它会发送空数组。 How can i make search case insensitive ? 如何使搜索不区分大小写?

searchService.js 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) { ...

You can use regex using .match(/pattern/i). 您可以使用.match(/ pattern / i)使用正则表达式。 The /i makes the pattern search case insensitive. / i使模式搜索不区分大小写。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM