根据stringtesting子string的数组

我有一个bot名称的数组。 当一个用户或机器人访问我的网站,我得到的user-agent并希望testing是否存在其中的一个值在我的数组中。

 var bots = [ "twitterbot", "linkedinbot", "facebookexternalhit", "pinterest", "dotbot", "rogerbot", "googlebot", "baiduspider", "yahoo! slurp", "bot", "spider", "simplepie", "yahooseeker", "embedly", "quora link preview", "outbrain", "vkshare", "monit", "pingability", "monitoring", "winhttprequest", "apache-httpclient", "getprismatic.com", "python-requests", "twurly", "yandex", "browserproxy", "webmeup-crawler", "qwantify" ]; var isBot = function(agent){ return bots.some(function(bot){ return bot.test(agent); }); } app.use(function(req, res, next){ var test = isBot(req.get("user-agent").toLowerCase()); console.log(test); }); 

这给了我错误: TypeError: undefined is not a function

这里怎么了? 奖励积分,如果你能帮助我扩大这个机器人列表指向我一个有用的外部链接或build议一些你自己!

       

网上收集的解决方案 "根据stringtesting子string的数组"

test函数在string上不可用,但在RegExp对象上不可用。 因此,您需要将您列入黑名单的项目定义为RegExps,而不是string:

  var bots = [ /twitterbot/, /linkedinbot/, /facebookexternalhit/, /pinterest/, /dotbot/, /rogerbot/, /googlebot/, /baiduspider/, /yahoo! slurp/, /bot/, /spider/, /simplepie/, /yahooseeker/, /embedly/, /quora link preview/, /outbrain/, /vkshare/, /monit/, /pingability/, /monitoring/, /winhttprequest/, /apache-httpclient/, /getprismatic.com/, /python-requests/, /twurly/, /yandex/, /browserproxy/, /webmeup-crawler/, /qwantify/ ]; var isBot = function(agent){ return bots.some(function(bot){ return bot.test(agent); }); }; alert('"clean-user-agent" is bot? ' + isBot('clean-user-agent')); alert('"the ***twitterbot***" is bot? ' + isBot('the ***twitterbot***')); 

尝试这个:

 var bots = [ /twitterbot/, /linkedinbot/, /facebookexternalhit/, /pinterest/, /dotbot/, /rogerbot/, /googlebot/, /baiduspider/, /yahoo! slurp/, /bot/, /spider/, /simplepie/, /yahooseeker/, /embedly/, /quora link preview/, /outbrain/, /vkshare/, /monit/, /pingability/, /monitoring/, /winhttprequest/, /apache-httpclient/, /getprismatic.com/, /python-requests/, /twurly/, /yandex/, /browserproxy/, /webmeup-crawler/, /qwantify/ ]; var isBot = function(agent){ for(var b in bots) { if(bot[b].test(agent)) { return true; } } return false; }