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. Lets assume i have text in file Lorem
now if i search as lorem
from client, it sends empty array because of case sensitive. How can i make search case insensitive ?
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). The /i makes the pattern search case insensitive.
if ("LOREMMMMMM".match(/Lorem/i)) console.log("Match");
上一篇: jQuery改变文本大小写
下一篇: 如何使用nodejs使搜索大小写不敏感?