hus*_*ain 0 javascript search node.js
我有一个搜索功能,所以我有来自客户端的输入,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();
});
Run Code Online (Sandbox Code Playgroud)
你可以利用 toLowerCase()
if (line.toLowerCase().indexOf(searchStr.toLowerCase()) != -1) { ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1190 次 |
| 最近记录: |