【问题标题】:How to check if string contains array values?如何检查字符串是否包含数组值?
【发布时间】:2014-01-30 12:25:30
【问题描述】:

我在编码中休息了几个月,现在我正试图重新开始。

我正在为 Facebook 游戏编写用户脚本,该脚本将从与游戏相关的所有帖子中收集数据,并使用该数据开始接受过程。

但是,我正在尝试为用户引入一个选项,让他们选择他们愿意接受的奖励类型,但我在应用它时遇到了麻烦。

我想要做的是获取帖子标题字符串,并将其与一组选定选项(复选框值)进行比较,以查看帖子标题是否包含数组中的任何值。

这是用户可以选择接受哪些帖子的选项面板:

$("#rightCol").prepend('<div id="bonus_options_panel">'
                       +'<ul id="bonuses"><legend><b>Select Bonuses to accept...</b></legend>'
                       +'<li><label>Hungry No More <input type="checkbox" value="heroic companions!" class="bonus_select_option"></label></li>'
                       +'<li><label>Pikemen <input type="checkbox" value="extra pikes!" class="bonus_select_option"></label></li>'
                       +'<li><label>Swordsmen <input type="checkbox" value="recruited swordsmen!" class="bonus_select_option"></label></li>'
                       +'<li><label>Valuable Treasure <input type="checkbox" value="valuable treasure!" class="bonus_select_option"></label></li>'
                       +'<li><label>Jousting Competition <input type="checkbox" value="jousting game!" class="bonus_select_option"></label></li>'
                       +'<li><label>Gamble <input type="checkbox" value="wants to gamble." class="bonus_select_option"></label></li>'
                       +'<li><label>Extra Lumber <input type="checkbox" value="has extra lumber!" class="bonus_select_option"></label></li>'
                       +'<li><label>Foreign Traders <input type="checkbox" value="attracted foreign traders!" class="bonus_select_option"></label></li>'
                       +'<li><label>Buxom Wenches <input type="checkbox" value="inviting friends over!" class="bonus_select_option"></label></li>'
                       +'<li><label>Plague <input type="checkbox" value="kingdom has the plague!" class="bonus_select_option"></label></li>'
                       +'<li><label>Legendary Bard <input type="checkbox" value="hosting a legendary bard!" class="bonus_select_option"></label></li>'
                       +'<li><label>Wild Horses <input type="checkbox" value="found wild horses!" class="bonus_select_option"></label></li>'
                       +'<li><label>Mercenary Armies <input type="checkbox" value="mercenary armies with you." class="bonus_select_option"></label></li>'
                       +'<li><label>Famed Hero <input type="checkbox" value="feasting with a famed hero!" class="bonus_select_option"></label></li>'
                       +'<li><label>Queen Parading <input type="checkbox" value="Queen is parading!" class="bonus_select_option"></label></li>'
                       +'<li><label>Wanted Criminal <input type="checkbox" value="caught a wanted criminal!" class="bonus_select_option"></label></li>'
                       +'<li><label>Rolling Logs <input type="checkbox" value="has extra logs!" class="bonus_select_option"></label></li>'
                       +'<li><label>Archery Game <input type="checkbox" value="holding a Archery game!" class="bonus_select_option"></label></li>'
                       +'</ul>'
                       +'</div>');

这里是一个帖子标题字符串的例子: 利亚姆艾伦需要英雄伙伴!

我一直在创建一个复选框值数组,如下所示:

var bonus = [];
$("input.bonus_select_option:checked").each(function(){
     bonus.push($(this).val());
 });

我现在想做的是,获取帖子的标题字符串,并检查它是否包含任何数组值,但这样做有问题。

任何帮助将不胜感激,并提前致谢!

【问题讨论】:

标签: javascript jquery arrays string compare


【解决方案1】:

试试 Array.join 和 String.search 方法:

var re = new RegExp('(' + bonus.join('|') + ')');
console.log('Liam Allan is need of heroic companions!'.search(re) != -1);  //  true  if contains

【讨论】:

    【解决方案2】:

    从您如何填充 bonus 变量开始:

      var title = 'Liam Allan is need of heroic companions!';
      var bonus = [];
      $("input.bonus_select_option:checked").each(function(){
            bonus.push($(this).val());
      });
      $.each(bonus, function(key, value){
            if(title.indexOf(value) > 0) ... //Do something 
      });
    

    例如:

      var title = 'Liam Allan is need of heroic companions!';
      var bonus = [];
      bonus.push('heroic companions!');
      bonus.push('recruited swordsmen!');
      bonus.push('feasting with a famed hero!');
    
      $.each(bonus, function(key, value){
        if(title.indexOf(value) > 0) alert(value + " found in title");
      });
    

    会回来

      heroic companions! found in title
    

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2013-11-12
      • 2015-02-28
      • 2018-10-12
      • 2018-05-04
      相关资源
      最近更新 更多