How to make search case insensitive using nodejs?
我有一个搜索功能,所以我有来自客户机
搜索服务.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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(' '); // 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(); }); |
你可以利用
1 | if (line.toLowerCase().indexOf(searchStr.toLowerCase()) != -1) { ... |
可以使用regex using.match(/pattern/i)。/i使模式搜索不区分大小写。